views:

36

answers:

1

Please help me with this code - I am basically going through a book and trying to create the examples myself but I can't get past this error. I've checked my LogCat and it says "java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'". I know there is probably some small detail I'm omitting that is causing this but I can't put my finger on it - it is almost an exact copy of a working program example given by the book. I am trying to create a simple ArrayAdapter and feed it a dummy list. Here are the three files of interest:

package com.example.bwett.SimpleList;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class SimpleList extends ListActivity {
String[] item = {"Item1", "Item2", "Item3", "Item4",
                 "Item5", "Item6", "Item7", "Item8"};
TextView mainLabel; 

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    setListAdapter(new ArrayAdapter<String> (this,
                                             R.layout.row,
                                             R.id.label,
                                             item));
    mainLabel = (TextView)findViewById(R.id.mainLabel);
}
public void onListItemClick(ListView parent, View v,
                            int position, long id){
    mainLabel.setText(item[position]);
}

}

MAIN.XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
    android:id="@+id/mainLabel"   
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/>
<ListView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false"/>
</LinearLayout>

ROW.XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
<ImageView
    android:id="@+id/ok"
    android:layout_width="22px"
    android:layout_height="fill_parent"
    android:padding="2px"
    android:src="@drawable/icon"/>

<TextView
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textSize="44sp"/>
</LinearLayout>
+3  A: 

Add to the <ListView> xml element:

android:id="@android:id/list"

The error is because Android relies on you to identify the list element in your layout if you're using ListActivity. See the ListActivity documentation for details.

Yoni Samlan
Thanks! This worked. I need to go over the documentation, I've never run across "@android:id/" before, just android:id="@+id/" and android:id="@id/" for when you want relative layouts. Thanks again!Ahh, now I understand:ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or list if it's in code).
stayinwett
Yup. @id and @+id are for referencing/defining your own IDs for your app to reference internally; @android:ids are system-defined ones (some of which have specific meanings for different kinds of built-in Views and Activities).
Yoni Samlan