ListFragment“尚未创建内容视图";在旋转 [英] ListFragment "content view not yet created" on Rotate

查看:101
本文介绍了ListFragment“尚未创建内容视图";在旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展的ListFragment,它带有onViewCreated()

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调用主要活动以附加一些侦听器,并将适配器发送到片段的listView(logFrag是活动对MyListFragment的引用)

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)

我不知道为什么尚未创建ListView,因为在Fragment的onCreateView方法已经返回之后以及在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.

推荐答案

将代码从onViewCreated移到onActivityCreated.通常,所有使用UI小部件的东西都应该在onActivityCreated中.

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

这篇关于ListFragment“尚未创建内容视图";在旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆