您的内容必须有一个 ListView,其 id 属性为‘ android.R.id.list’

我已经创建了一个像下面这样的 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);
}
}

正如你所看到的,我没有做任何其他的事情,但是我正在犯错误:

您的内容必须有一个 ListView,其 id 属性为‘ android.R.id.list’

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

有什么问题吗?

110297 次浏览

像这样重命名 ListView 的 id,

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

因为使用的是 ListActivity,所以 xml 文件必须指定关键字 机器人,同时向 ID 提及。

如果你需要一个定制的 ListView,而不是扩展一个 ListActivity,你必须简单地扩展一个 Activity,应该有相同的 ID 没有关键字 机器人

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

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

这应该能解决你的问题

我根据上面的反馈修正了这个问题,因为我一开始没法让它工作:

Activity _ main. 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>

继承 活动类而不是 清单活动类可以解决这个问题。

public class ExampleActivity extends Activity  {


@Override
public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);


setContentView(R.layout.mainlist);
}
}

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

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