【问题标题】:Listview error: "Your content must have a ListView whose id attribute is 'android.R.id.list'"Listview 错误:“您的内容必须有一个 id 属性为 'android.R.id.list' 的 ListView”
【发布时间】:2012-01-02 00:21:30
【问题描述】:

我在我的 logcat 中收到一条错误消息,提示“您的内容必须有一个 id 属性为 'android.R.id.list' 的 ListView”。我的代码编译并运行,但当我开始我的 listview 活动时强制关闭。我检查了许多类似的问题,这似乎是一个常见问题,但我仍然无法修复我的代码。

宣言:

    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;

        }
    }
}

我的 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>

我的列表项:

<?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>

【问题讨论】:

    标签: android android-intent android-listview android-relativelayout listviewitem


    【解决方案1】:

    您可能正在使用 ListActivity。

    在你的 firstlist.xml 中将 id 替换为:

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

    ListActivity 查找您在 xml 中为 @android:id/list 的 id R.android.id.list。


    也可以看看这个帖子:ListView whose id attribute is 'android.R.id.list' Error when I have the ListView id set correctly

    【讨论】:

    • 我想我不明白你的意思。看起来我已经在那里声明了你输入的列表视图
    • 当我复制并粘贴代码时,“lv = (ListView) lv.findViewById(R.id.list);”下出现错误.list 下的列表无法解析或不是字段
    • 请看一下你的声明:你写的是 android:id="@+id/list" 而我的是 android:id="@android:id/list"。那就是问题所在。正如我稍后阐述的那样,ID 是问题,而您的声明是错误的。
    • 谢谢你让我的 listview 工作,但它并没有按照我想要的方式显示在我的 getview 方法上。它像任何其他基本列表视图一样显示,而不是我的自定义列表。
    • 我开始了关于第二个问题的另一篇文章,以防你想检查一下
    猜你喜欢
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多