我得到了一个运行时异常
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>
发布于 2011-03-28 21:52:12
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
如果您仍然想使用ListActivity,这将解决该错误。
发布于 2010-06-15 03:54:25
删除对setContentView
的调用-您不需要在ListActivity中使用它,除非您正在做一些激进的事情。代码应该可以在没有它的情况下工作。
发布于 2012-07-02 23:10:00
另一种方法是,不要扩展ListActivity
。只需扩展Activity
,你就可以通过setContentView()
创建你的列表视图,并通过findViewById(R.id.yourlistview)
获得列表视图。
如果你从ListActivity
扩展,那么不要使用setContentView()
。您需要获取由getListView()
托管在列表活动中的默认列表视图。
https://stackoverflow.com/questions/3040374
复制相似问题