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

后端 未结 4 1876
不思量自难忘°
不思量自难忘° 2020-12-10 15:31

I know that there has been simialr questions but I can\'t make it work even though I look at those. The error message is:

java.lang.RuntimeException:

相关标签:
4条回答
  • 2020-12-10 15:47

    Change your ListView in XML to have the following id:

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

    It's a requirement of using 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)

    0 讨论(0)
  • 2020-12-10 15:55

    I decided to stop using actionbarsherlock and use the support libraries instead to get action bar in Android 2.1 and above. After doing this, i got the "java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'". What i did to remedy this problem was

    to read this http://developer.android.com/reference/android/app/ListFragment.html#q=fragment

    then change my fragment layout file from

    <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" />
    

    to

    <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"/>
    

    ... and voila, the RunTimeException with missing android.R.id.list was gone! Of course, i need to edit my new layout and fix it properly, but the key issue here was that i had overridden the fragment layout in its onCreateView method. ActionBarSherlock DID NOT have a problem with this, but with the Android support library, if u do this, you MUST have a ListView in your XML Layout, with the android:id="@id/android:list" as stated in the info linked to above.

    Hope this helps (coming from a total newb in Android app dev)!

    0 讨论(0)
  • 2020-12-10 15:59

    You can try to clean your project. If any xml releted error have , you can find it. Then you can change your list id . Find the list id are exist into your gen folder R.java class.

    0 讨论(0)
  • 2020-12-10 16:05

    The problem is here :

    android:id="@+id/list"
    

    you are adding a new id for your ListView but ListFragment needs a default Android ListView Id

    change your ListView Id to :

    <ListView
       android:id="@id/android:list"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />
    
    0 讨论(0)
提交回复
热议问题