158

我创建了一个这样的 xml 文件:

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

和一个活动:

public class ExampleActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainlist);
    }
}

如你所见,我没有做任何其他事情。但我得到了错误:

您的内容必须有一个 ID 属性为“android.R.id.list”的 ListView

即使android:id="@+id/list"我的 xml 中有该行。

问题是什么?

4

7 回答 7

348

像这样重命名 ListView 的 id,

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

由于您使用的是 xml 文件,因此必须在提及 ID 时ListActivity指定关键字android 。

如果您需要自定义ListView而不是扩展 a ListActivity,您必须简单地扩展 anActivity并且应该具有相同的 id 而没有关键字android

于 2012-06-15T12:41:31.763 回答
23

您的mainlist.xml文件中应该有一个列表视图,其 id 为@android:id/list

<ListView
    android:id="@android:id/list"
    android:layout_height="wrap_content"
    android:layout_height="fill_parent"/>
于 2012-06-15T12:41:45.710 回答
15
<ListView android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

这应该可以解决您的问题

于 2012-06-15T12:54:05.470 回答
8

我根据上面的反馈修复了这个问题的确切方法,因为我一开始无法让它工作:

活动主.xml:

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

MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addPreferencesFromResource(R.xml.preferences);

首选项.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
    android:key="upgradecategory"
    android:title="Upgrade" >
    <Preference
        android:key="download"
        android:title="Get OnCall Pager Pro"
        android:summary="Touch to download the Pro Version!" />
</PreferenceCategory>
</PreferenceScreen>
于 2014-01-09T05:15:14.887 回答
4

继承Activity类而不是ListActivity可以解决此问题。

public class ExampleActivity extends Activity  {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.mainlist);
    }
}
于 2014-11-22T10:13:11.337 回答
1
<ListView android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="false"
        android:scrollbars="vertical"/>
于 2018-02-06T04:17:22.747 回答
0

影响我的另一件事:如果您有多个测试设备,请确保您正在更改设备使用的布局。就我而言,我花了一段时间对“layout”目录中的 xmls 进行更改,直到我发现我的较大手机(我在测试中途切换到)正在使用“layout-sw360dp”目录中的 xmls。呸!

于 2016-03-13T01:34:30.783 回答