RuntimeException:您的内容必须具有ID属性为“ android.R.id.list”的ListView


87

我遇到了运行时异常

java.lang.RuntimeException:您的内容必须具有ID属性为“ android.R.id.list”的ListView

我不知道怎么了

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

15
一定要选择一个答案!
菲尔(Phil)

什么是mDbHelper?我在同一条船上,并试图使其工作我的ListView ...
Nick Kahn

Answers:


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

如果您仍要使用ListActivity,这将解决错误。


48
我必须使用其他格式,android:id="@+id/android:list" 如果无法使用,请尝试。您的班级仍然必须扩展ListActivity,而不是这样ListView yourListView = (ListView)findViewById(R.id.yourListView);做来初始化ListView yourListView = getListView();
CQM

@CQM这是这种行为的一种模式吗?
Rui Carneiro'4

5
如果OP愿意选择答案,这就是解决方案!
美国

1
为什么这样做?为什么我不能将其他任何值分配给android:id?
Vivek Pandey 2014年

1
@VivekPandey,因为您要扩展ListActivity,它需要一个带有android:id的ListView的ListView,所以developer.android.com/reference/android/app/ListActivity.html,zgcharley在下面清楚地解释了这一点
mirageservo

52

删除对setContentView- 的调用-除非您正在执行一些根本操作,否则您不需要在ListActivity中使用它。没有它,代码应该可以工作。


4
但是,如果我们不使用setContentView(),那么Activity如何知道要应用哪个模板?
ses 2013年

@ses,我相信它将使用预定义的ListView模板。
2016年

26

另一种方法是,不要扩展ListActivity。只是扩展Activity,然后您可以通过创建列表视图,setContentView()并通过获取列表视图findViewById(R.id.yourlistview)

如果从扩展ListActivity,则不要使用setContentView()。您需要通过来获得托管在列表活动中的默认列表视图getListView()


10

我收到的错误消息也有类似的问题,我发现这是因为我扩展了ListActivity而不是Activity(这是我从另一个项目重新利用代码的目的;))


6
<ListView android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="false"
        android:scrollbars="vertical"/>

3

我也面对这样。在我的情况下,在课堂上(也许不是你的情况下,只是与他人分享相关的)mainActivity.java yourClassName extends ListActivity(){...}改变yourClassName extends Activity(){...}


1
我发现这真的很有用。感谢Baras分享。
Ayoub

下一杯啤酒在我身上。
Venkata Tata

1

我也遇到了这个问题,我正在从listactivity扩展我的活动类,但是我的列表视图的id并不是默认值,所以我将其更改回list,现在可以正常工作了。阿西姆·索罗亚

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.