Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

Listview error: "Your content must have a ListView whose id attribute is 'android.R.id.list'"

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...