您的内容必须具有ListView,其id属性为'android.R.id.list'

Isa*_*uru 152 android android-listview

我创建了一个像这样的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>
Run Code Online (Sandbox Code Playgroud)

和一项活动:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

如你所见,我还没有做任何其他事情.但是我收到了错误:

您的内容必须具有ListView,其id属性为'android.R.id.list'

即使我android:id="@+id/list"在我的xml中有这条线.

问题是什么?

And*_*lva 338

像这样重命名ListView的id,

<ListView android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>
Run Code Online (Sandbox Code Playgroud)

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

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

  • 这样做确实解决了我的问题,谢谢.但是我想知道你是否会如此善意地写下一些关于何时ListView必须具有ID"@android:id/list"以及何时可以使用任意名称的解释.这是因为我不仅要修复我的问题,还要了解为什么需要修复. (4认同)
  • 作品.但是为什么这个带有id的'游戏'取决于视图? (3认同)

Pra*_*mar 23

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

<ListView
    android:id="@android:id/list"
    android:layout_height="wrap_content"
    android:layout_height="fill_parent"/>
Run Code Online (Sandbox Code Playgroud)


Aam*_*han 15

<ListView android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>
Run Code Online (Sandbox Code Playgroud)

这应该可以解决你的问题


Kev*_*vin 7

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

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>
Run Code Online (Sandbox Code Playgroud)

MainActivity.java:

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

的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>
Run Code Online (Sandbox Code Playgroud)