ListFragment错误旋转后尚未创建的内容视图

时间:2016-09-02 19:00:00

标签: android android-fragments android-listfragment

我有类ContentBrowseTransitionFragment,它扩展了ListFragment并覆盖onCreateView,onViewCreated方法。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_browse_transition, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    showFastScrollBar();
}

private void showFastScrollBar() {
    if (getFragment() != null && getFragment().getListView() != null) {
        if (_numItems > 50) {
            getFragment().getListView().setFastScrollEnabled(true);
            getFragment().getListView().setFastScrollAlwaysVisible(true);
        } else {
            getFragment().getListView().setFastScrollEnabled(false);
            getFragment().getListView().setFastScrollAlwaysVisible(false);
        }
    }
}

从onViewCreated()调用函数showFastScrollBar()时发生错误。

java.lang.IllegalStateException: Content view not yet created

at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)

at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)

at ...ContentBrowseListFragment.getListView(ContentBrowseListFragment.java:23)

at ...ContentBrowseTransitionFragment.showFastScrollBar(ContentBrowseTransitionFragment.java:541)

at ...ContentBrowseTransitionFragment.onViewCreated(ContentBrowseTransitionFragment.java:141)

at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1486)

at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)

at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)

at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)

at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)

at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)

at android.os.Handler.handleCallback(Handler.java:739)

at android.os.Handler.dispatchMessage(Handler.java:95)

at android.os.Looper.loop(Looper.java:135)

at android.app.ActivityThread.main(ActivityThread.java:5254)

at java.lang.reflect.Method.invoke(Native Method)

at java.lang.reflect.Method.invoke(Method.java:372)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

我已经阅读了这样的多个帖子,但没有任何帮助。我还阅读了帖子ListFragment "content view not yet created" on Rotate并尝试将函数showFastScrollBar()调用添加到onActivityCreated()但它没有帮助。提前感谢您的帮助。

0 个答案:

没有答案