【问题标题】:RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'RuntimeException:您的内容必须有一个 id 属性为 'android.R.id.list' 的 ListView
【发布时间】:2010-06-14 19:45:17
【问题描述】:

我遇到了运行时异常

java.lang.RuntimeException: 你的内容必须有一个 ID 为 ListView 属性是'android.R.id.list'

我不知道怎么了。

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

【问题讨论】:

  • 一定要选择答案!
  • 什么是 mDbHelper?我在同一条船上,并试图让它在我的 ListView 中工作......

标签: android


【解决方案1】:
<ListView android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

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

【讨论】:

  • 我不得不使用不同的格式,如果不起作用,请尝试android:id="@+id/android:list"。您的类仍必须扩展 ListActivity,而不是执行 ListView yourListView = (ListView)findViewById(R.id.yourListView); 来初始化 ListView yourListView = getListView();
  • @CQM 这种行为有什么模式吗?
  • 如果 OP 愿意选择答案,这就是解决方案!
  • 为什么会这样?为什么我不能为 android:id 分配任何其他值?
  • @VivekPandey,因为您正在扩展 ListActivity,它需要一个带有 android:id 列表的 ListView,developer.android.com/reference/android/app/ListActivity.html,下面的 zgcharley 解释清楚了
【解决方案2】:

删除对setContentView 的调用 - 在 ListActivity 中不需要它,除非你正在做一些激进的事情。没有它,代码应该可以工作。

【讨论】:

  • 但是如果我们不使用 setContentView(),Activity 怎么知道应用什么模板呢?
  • @ses,我相信它会使用预定义的 ListView 模板。
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

    我对您收到的错误消息也有类似的问题,我发现这是因为我正在扩展 ListActivity 而不仅仅是 Activity(这就是我从不同项目中重新利用代码所得到的;))

    【讨论】:

      【解决方案5】:
      <ListView android:id="@id/android:list"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:drawSelectorOnTop="false"
              android:scrollbars="vertical"/>
      

      【讨论】:

        【解决方案6】:

        我也是这样的。在我的情况下(可能与你的情况无关,只是分享给其他人)在课堂上mainActivity.java yourClassName extends ListActivity(){...}改成yourClassName extends Activity(){...}

        【讨论】:

        • 我发现这真的很有用。感谢巴拉斯分享这个。
        • 下一杯啤酒在我身上。
        【解决方案7】:

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

        【讨论】:

          猜你喜欢
          • 2012-06-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多