Android – 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);
    if (mAdapter != null) {

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

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
11-22 14:58:24.336: E/AndroidRuntime(22261):    at
11-22 14:58:24.336: E/AndroidRuntime(22261):    at com.berrmal.timetracker.MainActivity.populateLogFrag(
11-22 14:58:24.336: E/AndroidRuntime(22261):    at com.berrmal.timetracker.MainActivity.onFragmentAction(
11-22 14:58:24.336: E/AndroidRuntime(22261):    at com.berrmal.timetracker.MyListFragment.onViewCreated(

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.

Best Solution

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

Related Question