【问题标题】:Your content must have a ListView whose id attribute is 'android.R.id.list'您的内容必须有一个 ID 属性为“android.R.id.list”的 ListView
【发布时间】:2012-06-18 12:46:15
【问题描述】:

我已经创建了一个这样的 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

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

有什么问题?

【问题讨论】:

    标签: android android-listview


    【解决方案1】:

    像这样重命名 ListView 的 id,

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

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

    如果您需要自定义ListView,则不必扩展ListActivity,您只需扩展Activity,并且应该具有相同的ID,但没有关键字android

    【讨论】:

    • 这样做确实解决了我的问题,谢谢。但是我想知道您是否愿意写几句话来解释一下 ListView 何时必须具有 ID“@android:id/list”以及何时可以使用任意名称。这是因为我不仅喜欢解决我的问题,而且还想了解为什么需要修复。
    • 有效。但是为什么这个带有 id 的“游戏”取决于视图 impl?
    • 当我添加 @android 时,Eclipse 会显示 list cannot be resolved or is not a field
    • 同样的问题,它显示错误:错误:找不到与给定名称匹配的资源(在“id”处,值为“@android:id/list_interestsent”)。
    • 感谢您的精彩回答!
    【解决方案2】:

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

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

    【讨论】:

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

      这应该可以解决你的问题

      【讨论】:

        【解决方案4】:

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

        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);
        

        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>
        

        【讨论】:

          【解决方案5】:

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

          public class ExampleActivity extends Activity  {
          
              @Override
              public void onCreate(Bundle savedInstanceState) {
          
                  super.onCreate(savedInstanceState);
          
                  setContentView(R.layout.mainlist);
              }
          }
          

          【讨论】:

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

            【讨论】:

              【解决方案7】:

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

              【讨论】:

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