使用ViewPager的ActionBar - 单击部分可见选项卡会使应用程序崩溃

时间:2013-09-06 03:46:08

标签: android android-actionbar android-viewpager

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

enter image description here

我的问题是,最初有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个片段吗?或者,如何正确处理最后一个片段的加载?

视图寻呼机和那些片段周围有很多代码,我不确定需要多少代码才能回答我的问题。如果我需要在此发布完整代码,请告诉我。

1 个答案:

答案 0 :(得分:2)

在其中一个回调(例如Loader)中启动onActivityCreated(),> onCreateView()后运行,以确保{{1}以非常快的速度返回数据不会使您处于尚未创建Loader视图的位置(在您的情况下出现问题)。