Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListFragment "content view not yet created" on Rotate

I have an extended ListFragment, with this onViewCreated()

public void onViewCreated(View v,Bundle savedInstanceState) {
    super.onViewCreated(v, savedInstanceState);
    mListener.onFragmentAction(0x000A);
    if (mAdapter != null) {
        getListView().setAdapter(mAdapter);
    }
}

mListener.onFragmentAction calls the main activity to attach some listeners and send the adapter to the fragment's listView (logFrag is the activity's reference to MyListFragment)

public void populateLogFrag() {
//line 225  logFrag.getListView().setOnItemLongClickListener(myEventListener);
            logFrag.getListView().setOnItemClickListener(myEventListener);
            refreshLogAdapter(provideLogAdapter());
}

The app loads fine, but after config change (device rotation) I get the following stack trace:

11-22 14:58:24.336: E/AndroidRuntime(22261): Caused by: java.lang.IllegalStateException: Content view not yet created
11-22 14:58:24.336: E/AndroidRuntime(22261):    at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
11-22 14:58:24.336: E/AndroidRuntime(22261):    at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
11-22 14:58:24.336: E/AndroidRuntime(22261):    at com.berrmal.timetracker.MainActivity.populateLogFrag(MainActivity.java:225)
11-22 14:58:24.336: E/AndroidRuntime(22261):    at com.berrmal.timetracker.MainActivity.onFragmentAction(MainActivity.java:282)
11-22 14:58:24.336: E/AndroidRuntime(22261):    at com.berrmal.timetracker.MyListFragment.onViewCreated(MyListFragment.java:23)

I can't figure out why the ListView is not yet created, since I'm calling it after the Fragment's onCreateView method has already returned, and during onViewCreated(). I found a thread or two with related titles, but the solutions didn't seem to apply here.

like image 828
nexus_2006 Avatar asked Nov 22 '13 23:11

nexus_2006


1 Answers

Move the code from onViewCreated to onActivityCreated. Generally, everything that uses UI widgets should be in onActivityCreated.

like image 100
Szymon Avatar answered Oct 29 '22 00:10

Szymon