ListFragment “content view not yet created” on Rotate

时光总嘲笑我的痴心妄想 提交于 2019-12-06 20:27:38

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/20156197/listfragment-content-view-not-yet-created-on-rotate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!