9

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: Your content must have a ListView whose id attribute is 'android.R.id.list'

. I have tried to change the id to list. In another. This what the code looks like:

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

It's using the following adapter:

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

Here is the xml-file that specifies the list(fragment_committee):

<?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>

Here is the xml for the list item:

 <?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>

I hope that you can figure out what's wrong. I have another listview that has the same id (list) in another xml-file within the project. I don't know if that's a problem. Please help me solve the problem.

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
Filip Eriksson
  • 975
  • 7
  • 30
  • 47

4 Answers4

23

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" />
Arash GM
  • 10,316
  • 6
  • 58
  • 76
5

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)

Ken Wolf
  • 23,133
  • 6
  • 63
  • 84
0

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.

Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37
0

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

TiredDude
  • 101
  • 1
  • 6
  • I would like to add to this info that since i was using ListFragment, i DID NOT have a ListView tag in XML anywhere, for this ListFragment. The ListView is built-in to the ListFragment but can be overridden. Therefore i could not just change the ListView android:id as many people suggest, because i could not find any ListView tags in XML related to my ListFragment. Hence the above solution - to add one! – TiredDude Oct 03 '13 at 22:03
  • Another comment to my own post above: I'm such a total newb to this and think i've used a ListFragment when i did not want a list in the first place. Makes sense that a ListFragment needs at least one ListView. I'm gonna use another type of Fragment so that i don't have to implement an unused ListView in my XML just to get rid of the RunTimeException. Gaah! – TiredDude Oct 09 '13 at 14:10