列表视图错误:"你的内容必须有一个ListView,其id属性是'android.R.id.list'" [英] Listview error: "Your content must have a ListView whose id attribute is 'android.R.id.list'"

查看:102
本文介绍了列表视图错误:"你的内容必须有一个ListView,其id属性是'android.R.id.list'"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个错误在我的logcat中说:你的内容必须有一个ListView,其id属性是'android.R.id.list'。我的code编译和运行,但是当我开始我的列表视图活动力关闭。我已经在许多类似的问题被选中,这似乎是一个常见的​​问题,但我仍然无法修复我的code。

宣言:

 私人的ListView LV;
    语境mContext;
    名单mList;
    的String [] testcontacts;

    消息查看AA = NULL;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.firstlist);
        testcontacts = getResources()
                .getStringArray(R.array.testcontacts_array);

        AA =新的消息查看();
        的lv = getListView(); / *(的ListView)lv.findViewById(R.id.list);不得不注释掉,它无法找到列表* /
        lv.setAdapter(AA);
        lv.setTextFilterEnabled(真正的);

        lv.setOnItemClickListener(新OnItemClickListener(){
            公共无效onItemClick(适配器视图<>母公司视图中查看,
                    INT位置,长的id){
                //单击时,显示出与TextView的文字敬酒
                Toast.makeText(getApplicationContext(),
                        ((TextView中)查看).getText(),Toast.LENGTH_SHORT).show();
            }
        });
    }
类消息查看扩展ArrayAdapter<字符串> {
        消息查看(){
            超(FirstLoginActivity.this,android.R.layout.activity_list_item,
                    testcontacts);
            // TODO自动生成构造函数存根
        }

        公共查看getView(INT位置,查看convertview,ViewGroup中父){
            Log.d(EBZ,getView方法内);
            ViewHolder持有人;
            视图V = convertview;
            如果(V == NULL){
                Log.d(EBZ,如果v ==空);
                LayoutInflater充气= getLayoutInflater();
                V = inflater.inflate(R.layout.list_items,NULL);
                持有人=新ViewHolder();
                holder.firstLine =(TextView中)v.findViewById(R.id.firstLine);
                holder.secondLine =(TextView中)v.findViewById(R.id.secondLine);
                holder.icon1 =(ImageView的)v.findViewById(R.id.icon1);
                holder.icon2 =(ImageView的)v.findViewById(R.id.icon2);
                v.setTag(保持器);
            } 其他 {
                支架=(ViewHolder)v.getTag();
            }
            holder.firstLine.setText(testcontacts [位置]);
            holder.secondLine.setText(testcontacts [位置]);
            holder.icon1.setImageBitmap(空);
            holder.icon2.setImageBitmap(空);
            //直接调用图像?
            返回伏;
        }

        类ViewHolder {
            TextView的firstLine中;
            TextView的secondLine;
            ImageView的ICON1;
            ImageView的图标2;

        }
    }
}
 

我的XML

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直>

    < RelativeLayout的
        机器人:ID =@ + ID / top_control_bar
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT>

        <的TextView
            机器人:ID =@ + ID / textView1
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_marginBottom =10dp
            机器人:layout_weight =1
            机器人:后台=#cc252a
            机器人:paddingBottom会=10dp
            机器人:以下属性来=10dp
            机器人:paddingTop =10dp
            机器人:文本=这将改变
            机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceLarge?
    < / RelativeLayout的>

    <的LinearLayout
        机器人:ID =@ + ID / bottom_control_bar
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentBottom =真正的>
    < / LinearLayout中>

    <的ListView
        机器人:ID =@ + ID /列表
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =0dip
        机器人:layout_above =@ ID / bottom_control_bar
        机器人:layout_below =@ ID / top_control_bar
        机器人:choiceMode =multipleChoice/>

< / RelativeLayout的>
 

我的ListItems的:

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直>

    < RelativeLayout的
        机器人:ID =@ + ID / top_control_bar
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT>


        <的TextView
            机器人:ID =@ + ID / textView1
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_marginBottom =10dp
            机器人:layout_weight =1
            机器人:后台=#cc252a
            机器人:paddingBottom会=10dp
            机器人:以下属性来=10dp
            机器人:paddingTop =10dp
            机器人:文本=这将改变
            机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceLarge?

    < / RelativeLayout的>

    <的LinearLayout
        机器人:ID =@ + ID / bottom_control_bar
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentBottom =真正的>
    < / LinearLayout中>

    <的ListView
        机器人:ID =@ + ID /列表
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =0dip
        机器人:layout_above =@ ID / bottom_control_bar
        机器人:layout_below =@ ID / top_control_bar
        机器人:choiceMode =multipleChoice
        机器人:分隔=#cc252a
        机器人:dividerHeight =14.5dp/>

< / RelativeLayout的>
 

解决方案

您可能正在使用一个ListActivity。

在你firstlist.xml更换ID为:

 <的ListView
    机器人:ID =@机器人:ID /列表
...
 

ListActivity查找ID R.android.id.list你的XML是@android:ID /列表


另外,也要看看这篇文章:<一个href="http://stackoverflow.com/questions/5495603/listview-whose-id-attribute-is-android-r-id-list-error-when-i-have-the-listvie">ListView其id属性为android.R.id.list错误时,我有ListView控件ID设置正确

I am getting an error in my logcat saying "Your content must have a ListView whose id attribute is 'android.R.id.list'". My code compiles and runs but force closes when I start my listview activity. I've checked on many similar questions, it seems to be a common problem but I am still unable to fix my code.

The Declaration:

    private ListView lv;
    Context mContext;
    List mList;
    String[] testcontacts;

    MessageView aa = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.firstlist);
        testcontacts = getResources()
                .getStringArray(R.array.testcontacts_array);

        aa = new MessageView();
        lv = getListView();/*(ListView) lv.findViewById(R.id.list); Had to comment out, it cannot find list*/
        lv.setAdapter(aa);
        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // When clicked, show a toast with the TextView text
                Toast.makeText(getApplicationContext(),
                        ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }
class MessageView extends ArrayAdapter<String> {
        MessageView() {
            super(FirstLoginActivity.this, android.R.layout.activity_list_item,
                    testcontacts);
            // TODO Auto-generated constructor stub
        }

        public View getView(int position, View convertview, ViewGroup parent) {
            Log.d("Ebz", "inside getView method");
            ViewHolder holder;
            View v = convertview;
            if (v == null) {
                Log.d("Ebz", "if v == null");
                LayoutInflater inflater = getLayoutInflater();
                v = inflater.inflate(R.layout.list_items, null);
                holder = new ViewHolder();
                holder.firstLine = (TextView) v.findViewById(R.id.firstLine);
                holder.secondLine = (TextView) v.findViewById(R.id.secondLine);
                holder.icon1 = (ImageView) v.findViewById(R.id.icon1);
                holder.icon2 = (ImageView) v.findViewById(R.id.icon2);
                v.setTag(holder);
            } else {
                holder = (ViewHolder) v.getTag();
            }
            holder.firstLine.setText(testcontacts[position]);
            holder.secondLine.setText(testcontacts[position]);
            holder.icon1.setImageBitmap(null);
            holder.icon2.setImageBitmap(null);
            // call the images directly?
            return v;
        }

        class ViewHolder {
            TextView firstLine;
            TextView secondLine;
            ImageView icon1;
            ImageView icon2;

        }
    }
}

My XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/top_control_bar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_weight="1"
            android:background="#cc252a"
            android:paddingBottom="10dp"
            android:paddingLeft="10dp"
            android:paddingTop="10dp"
            android:text="This will be Changed"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/bottom_control_bar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >
    </LinearLayout>

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_above="@id/bottom_control_bar"
        android:layout_below="@id/top_control_bar"
        android:choiceMode="multipleChoice" />

</RelativeLayout>

My ListItems:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/top_control_bar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >


        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_weight="1"
            android:background="#cc252a"
            android:paddingBottom="10dp"
            android:paddingLeft="10dp"
            android:paddingTop="10dp"
            android:text="This will be Changed"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/bottom_control_bar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >
    </LinearLayout>

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_above="@id/bottom_control_bar"
        android:layout_below="@id/top_control_bar"
        android:choiceMode="multipleChoice"
        android:divider="#cc252a"
        android:dividerHeight="14.5dp" />

</RelativeLayout>

解决方案

You are probably using a ListActivity.

In you firstlist.xml replace the id to:

<ListView
    android:id="@android:id/list"
...

ListActivity looks for the id R.android.id.list which you in xml is @android:id/list.


Also look at this post: ListView whose id attribute is 'android.R.id.list' Error when I have the ListView id set correctly

这篇关于列表视图错误:&QUOT;你的内容必须有一个ListView,其id属性是'android.R.id.list'&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆