RuntimeException: 您的内容必须有一个 id 属性为“android.R.id.list”的 ListView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3040374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
提问by raji
I am getting a run time exception
我收到运行时异常
java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
java.lang.RuntimeException: 您的内容必须有一个 id 属性为“android.R.id.list”的 ListView
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
<?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
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>
回答by Nic
<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.
如果您仍想使用 ListActivity,这将解决错误。
回答by Ben L.
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.
删除对的调用setContentView
- 除非您正在做一些激进的事情,否则您不需要在 ListActivity 中使用它。没有它,代码应该可以工作。
回答by zgcharley
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)
.
另一种方法是,不要扩展ListActivity
。只需扩展Activity
,然后您就可以创建您的列表视图setContentView()
并通过获取列表视图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()
.
如果您从 扩展ListActivity
,则不要使用setContentView()
. 您需要通过 获取在列表活动中托管的默认列表视图getListView()
。
回答by Tom
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 ;) )
我收到的错误消息也有类似的问题,我发现这是因为我正在扩展 ListActivity 而不仅仅是 Activity(这是我从不同项目重新利用代码所得到的;))
回答by kid
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:scrollbars="vertical"/>
回答by Sen
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(){...}
我也是这样的脸。在我的情况下(可能与您的情况无关,只是分享给他人)在课堂上mainActivity.java
yourClassName extends ListActivity(){...}
更改为yourClassName extends Activity(){...}
回答by Asim Soroya
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
我也遇到了这个问题,我从 listactivity 扩展了我的活动类,但是我的列表视图的 id 不是默认的,我把它改回了列表,现在它工作正常。阿西姆·索罗亚