Search code examples
androidandroid-fragmentsandroid-listviewandroid-sqliteandroid-listfragment

How to load data to a ListFragment?


I'm trying to load some data into a ListFragment but I'm getting this error:

05-07 00:05:30.533   625-625/com.myapp.android E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

How can I handle this?

Here are my files, if they can help:

MainActivity.java

public class MainActivity extends FragmentActivity {

private ViewPager viewPager;
private ViewpagerAdapter viewpagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    viewPager = (ViewPager) findViewById(R.id.pager);
    viewpagerAdapter = new ViewpagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(viewpagerAdapter);

}

}

ListFrag.java

public class ListFrag extends ListFragment {

private DatabaseAdapter databaseAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_listagem, container, false);
    return view;

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    databaseAdapter = new DatabaseAdapter(getActivity());
    databaseAdapter.open();

    List<Foo> values = databaseAdapter.getAllFoobar();

    //ArrayAdapter<Foo> adapter = new ArrayAdapter<Foo>(getActivity(), android.R.layout.simple_list_item_1, values);
    //setListAdapter(adapter);

}

}

And fragment.xml

<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="com.myapp.android.ListFrag">

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

</FrameLayout>

Thanks in advance!


Solution

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

    Crash log clearly says the issue . You need to change the ListView id . ListFragment expects the layout to have ListView id as @android:id/list

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

    If you look at the ListFragment source code , its checks for the ListView in the layout using android.R.id.list ID. If the view is null it will throw an RuntimeException

          View rawListView = root.findViewById(android.R.id.list);
            if (!(rawListView instanceof ListView)) {
                if (rawListView == null) {
                    throw new RuntimeException(
                            "Your content must have a ListView whose id attribute is " +
                            "'android.R.id.list'");
                }
                throw new RuntimeException(
                        "Content has view with id attribute 'android.R.id.list' "
                        + "that is not a ListView class");
            }
    

    Use this Layout

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >
    
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>