【问题标题】:Android listview error: Your content must have a ListView whose id attribute is 'android.R.id.list' but i have two listviews [duplicate]Android listview 错误:您的内容必须有一个 ID 属性为“android.R.id.list”的 ListView,但我有两个 listview [重复]
【发布时间】:2020-09-15 15:15:44
【问题描述】:

我有一个从 url 获取数据然后将其放入列表视图的应用程序。我想从两个 url 执行此操作,因此我有两个片段和两个 xml 文件,其中会发生这种情况。但我得到了错误 Your content must have a ListView whose id attribute is 'android.R.id.list' 我将 id 设置为在我的 xml 文件中列出,但是当有两个 xml 文件时不会有问题吗?我不能拥有相同的 id 对吗?

我的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ListFragment">

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

</FrameLayout> 

<?xml version="1.0" encoding="utf-8"?>
<Framelayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".DetailsFragment">

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

</Framelayout>

【问题讨论】:

    标签: android listview android-fragments


    【解决方案1】:

    不,它不会产生任何问题,因为android会在运行时解决重复问题,而且listview已经过时了,你应该开始使用recyclerview,它比listview好得多。

    【讨论】:

      【解决方案2】:

      你的Activity.有问题可能是你扩展ListActivity.

      您必须扩展 Activity 才能解决您的问题

      第二种解决方案是 将 id 定义为以下格式

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

      【讨论】:

      • 解决了您的问题?
      • 谢谢,是id!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-18
      • 1970-01-01
      相关资源
      最近更新 更多