java.lang.RuntimeException:您的内容必须具有ID属性为” android.R.id.list”的ListView

java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

我知道有类似的问题,但是即使我看那些问题也无法解决。错误消息是:

java.lang.RuntimeException: Your content must have a ListView whose id
attribute is 'android.R.id.list'

。我试图将ID更改为list。在另一个。代码如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 public class committeeFragment extends SherlockListFragment {

    private CommitteeAdapter adapter;

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        adapter = new CommitteeAdapter(getActivity().getApplicationContext());
        setListAdapter(adapter);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        return inflater.inflate(R.layout.fragment_committee, container, false);
    }

    public void
    refreshList() {
        adapter.updateDataSet();
        adapter.notifyDataSetChanged();
    }
}

它正在使用以下适配器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
    public class CommitteeAdapter extends BaseAdapter {
    private
    ArrayList<StudentCommittee> committeeList;
    private Context context;

    public CommitteeAdapter(Context context) {
        this.context = context;
        CommitteeDatabaseAdapter dbAdapter = new CommitteeDatabaseAdapter(context);
        committeeList = dbAdapter.readAllCommittees();
    }

    public void updateDataSet() {
        CommitteeDatabaseAdapter dbAdapter = new CommitteeDatabaseAdapter(context);
        committeeList = dbAdapter.readAllCommittees();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        StudentCommittee committee = committeeList.get(position);

        View v = vi.inflate(R.layout.list_item_committee, null);
        TextView sectionName = (TextView) v.findViewById(R.id.committee_namn);

        sectionName.setText(committee.getName());
        return v;
    }

    @Override
    public int getCount() {
        return committeeList.size();
    }

    @Override
    public StudentCommittee getItem(int position) {
        return committeeList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}

这是指定列表的XML文件(fragment_committee):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?xml version="1.0" encoding="utf-8"?> <LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
     android:tag="@string/tag_fragment_committee"
     android:background="@color/white">

     <TextView
         style="@style/AppsolutText.Headline"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="Festerier" />

      <View
         android:layout_width="fill_parent"
         android:layout_height="2dp"
         android:background="@drawable/divider_sliding_menu_item"/>

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

     <TextView
         style="@style/AppsolutText.Normal"
         android:id="@android:id/empty"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:text="Uppdatera f??r att se en lista ??ver festerier och fadderier. Kr?¤ver internetanslutning."/>

 </LinearLayout>

这是列表项的xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 <?xml version="1.0" encoding="utf-8"?> <LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="horizontal">

     <ImageView
         android:id="@+id/committee_symbol"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="0"
         android:paddingBottom="10dp"
         android:paddingRight="10dp"
         android:paddingTop="10dp" />

     <TextView
         android:id="@+id/committee_namn"
         style="@style/AppsolutText.ListHeader"
         android:layout_width="0dp"
         android:layout_height="wrap_content"
         android:layout_weight="1" />

 </LinearLayout>

我希望您能找出问题所在。我在项目中的另一个xml文件中有另一个具有相同ID(列表)的listview。我不知道这是不是一个问题。请帮助我解决问题。


问题在这里:

1
android:id="@+id/list"

您要为ListView添加新的ID,但ListFragment需要默认的Android ListView ID

将您的ListView ID更改为:

1
2
3
4
<ListView
   android:id="@id/android:list"
   android:layout_width="match_parent"
   android:layout_height="wrap_content" />


将XML中的ListView更改为具有以下ID:

1
2
3
4
<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

这是使用ListFragment / SherlockListFragment

的要求

http://developer.android.com/reference/android/app/ListFragment.html

ListFragment has a default layout that consists of a single list view. However, if you desire, you can customize the fragment layout by returning your own view hierarchy from onCreateView(LayoutInflater, ViewGroup, Bundle). To do this, your view hierarchy must contain a ListView object with the id"@android:id/list" (or list if it's in code)


我决定停止使用actionbarsherlock,而改用支持库来获取Android 2.1及更高版本中的action bar。完成此操作后,我得到了" java.lang.RuntimeException:您的内容必须具有ID属性为'android.R.id.list'的ListView"。我为解决此问题所做的事情是

阅读此http://developer.android.com/reference/android/app/ListFragment.html#q=fragment

然后从

更改我的片段布局文件

1
2
3
4
5
6
7
8
9
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/item_detail"
    style="?android:attr/textAppearanceLarge"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:textIsSelectable="true"
    tools:context=".ItemDetailFragment" />

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:orientation="vertical"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:paddingLeft="8dp"
     android:paddingRight="8dp"
     tools:context=".ItemDetailFragment">

 <ListView android:id="@id/android:list"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:background="#00FF00"
           android:layout_weight="1"
           android:drawSelectorOnTop="false"/>

 <TextView android:id="@+id/item_detail"
        style="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp"
        android:textIsSelectable="true"/>

 <TextView android:id="@id/android:empty"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:background="#FF0000"
           android:text="No data"/>

...,瞧,缺少android.R.id.list的RunTimeException不见了!当然,我需要编辑我的新布局并正确修复它,但是这里的关键问题是我已经在其onCreateView方法中覆盖了片段布局。 ActionBarSherlock DID对此没有问题,但是对于Android支持库,如果这样做,则必须在XML布局中具有ListView,如信息中所述android:id =" @ id / android:list"链接到上面。

希望这会有所帮助(来自Android应用程序开发人员中的newb)!


您可以尝试清理您的项目。如果有任何xml重新删除的错误,您可以找到它。然后,您可以更改列表ID。查找列表ID是否存在于您的gen文件夹R.java类中。