87

I am getting a run time exception

java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

I don't know what is wrong.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.newslist);
    mDbHelper.open();
    fillData();
}

private void fillData() {
    Bundle extras = getIntent().getExtras();
    long catID = extras.getLong("cat_id");
    Cursor c = mDbHelper.fetchNews(catID);
    startManagingCursor(c);

    String[] from = new String[] { DBHelper.KEY_TITLE };
    int[] to = new int[] { R.id.newslist_text };

    SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.text_newslist, c, from, to);
    setListAdapter(notes);
}

newslist.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
    <ListView 
         android:id="@+id/catnewslist"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content">
    </ListView>
</LinearLayout>

text_newslist.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView 
        android:text="@+id/newslist_text"
        android:id="@+id/newslist_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">    
    </TextView>
</LinearLayout>
zyamys
  • 1,609
  • 1
  • 21
  • 23
raji
  • 1,011
  • 2
  • 9
  • 9

7 Answers7

189
<ListView android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

This will solve the error if you still want to use a ListActivity.

Nic
  • 2,864
  • 1
  • 16
  • 21
  • 50
    I had to use a different format, try `android:id="@+id/android:list"` if it doesn't work. Your class must still extend ListActivity, and instead of doing `ListView yourListView = (ListView)findViewById(R.id.yourListView);` do this to initialize `ListView yourListView = getListView();` – CQM Oct 12 '11 at 16:01
  • @CQM here is any kind of pattern on this behavior? – Rui Carneiro Apr 21 '12 at 03:04
  • 5
    This is the solution, if the OP ever cared to select an answer! – States Oct 16 '12 at 23:40
  • 1
    Why does this work? Why can't I assign any other value to android:id? – Vivek Pandey Sep 23 '14 at 17:06
  • 1
    @VivekPandey, because you are extending the ListActivity which requires a ListView with an android:id of list, http://developer.android.com/reference/android/app/ListActivity.html, zgcharley below explained it clearly – mirageservo Mar 06 '15 at 10:28
  • This is one of those sources of bugs where you think to yourself "who thought that this was obvious and why?". – aclima Dec 30 '15 at 18:05
53

Remove the call to setContentView - you don't need it in a ListActivity unless you're doing something radical. The code should work without it.

Ben L.
  • 1,735
  • 1
  • 14
  • 19
27

Another way is, don't extend ListActivity. Just extends Activity, then you can create your list view by setContentView() and get the list view by findViewById(R.id.yourlistview).

If you extends from ListActivity, then don't use setContentView(). You need get the default list view hosted in list activity by getListView().

zgcharley
  • 1,084
  • 3
  • 17
  • 24
10

I had a similar issue with the error message you received, and I found it was because I was extending ListActivity instead of just Activity (that's what I get for re-purposing code from a different project ;) )

Tom
  • 101
  • 1
  • 3
6
<ListView android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="false"
        android:scrollbars="vertical"/>
kid
  • 61
  • 1
  • 1
3

I face like this too. In my case (Maybe not relevant with your case, just share to others) in class mainActivity.java yourClassName extends ListActivity(){...} change to yourClassName extends Activity(){...}

Sen
  • 154
  • 1
  • 11
1

I also faced this issue, I was extending my activity class from listactivity, but the id of my list view was not defualt I changed it back to list and now its working fine. Asim soroya