2

我正在使用 ViewPager 来显示 4 个不同的片段。我使用操作栏来显示导航选项卡。它看起来像这样:

在此处输入图像描述

我的问题是最初 3 个选项卡是完全可见的,最后一个是部分可见的。单击前 3 个选项卡中的任何一个都可以,但是当我单击最后一个选项卡时,应用程序崩溃:

09-06 13:35:01.825: E/AndroidRuntime(6676): FATAL EXCEPTION: main
09-06 13:35:01.825: E/AndroidRuntime(6676): java.lang.IllegalStateException: Content view not yet created
09-06 13:35:01.825: E/AndroidRuntime(6676):     at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at com.szymontrapp.englishverbs.DictionaryListFragment.onLoadFinished(DictionaryListFragment.java:200)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at com.szymontrapp.englishverbs.DictionaryListFragment.onLoadFinished(DictionaryListFragment.java:1)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:427)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at android.support.v4.app.LoaderManagerImpl.initLoader(LoaderManager.java:562)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at com.szymontrapp.englishverbs.DictionaryListFragment.onCreate(DictionaryListFragment.java:94)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at android.support.v4.app.Fragment.performCreate(Fragment.java:1455)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:893)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:823)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:819)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1066)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:550)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:509)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:490)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at com.szymontrapp.englishverbs.DictionaryActivity$4.onTabSelected(DictionaryActivity.java:91)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at android.support.v7.app.ActionBarImplICS$TabWrapper.onTabSelected(ActionBarImplICS.java:454)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:587)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at com.android.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:1084)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at com.android.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:532)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at android.view.View.performClick(View.java:4211)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at android.view.View$PerformClick.run(View.java:17267)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at android.os.Handler.handleCallback(Handler.java:615)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at android.os.Looper.loop(Looper.java:137)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at android.app.ActivityThread.main(ActivityThread.java:4898)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at java.lang.reflect.Method.invokeNative(Native Method)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at java.lang.reflect.Method.invoke(Method.java:511)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
09-06 13:35:01.825: E/AndroidRuntime(6676):     at dalvik.system.NativeStart.main(Native Method)

似乎不知何故最初没有加载最后一个片段。我正在使用扩展 FragmentPageAdapter 的自定义类,并且仅对前 3 个调用 GetItem 方法。

我应该尝试强制加载所有 4 个片段吗?或者,如何正确处理最后一个片段的加载?

视图寻呼机和那些片段周围有很多代码,我不确定需要多少代码来回答我的问题。让我知道是否需要在此处发布完整代码。

4

1 回答 1

2

启动在调用之后运行的Loader回调之一(如onActivityCreated()),因此您确保非常快速地返回数据不会使您处于尚未创建视图的位置(您的情况下的问题)。onCreateView()LoaderFragment

于 2013-09-06T06:41:04.960 回答