FragmentStatePagerAdapter不适用于ChildFragmentManager

时间:2017-10-12 10:59:04

标签: android android-fragments fragmentstatepageradapter fragmentmanager

我有一个带有FragmentStatePagerAdapter的Activity,但我必须使用NavigationView,所以我必须将Activities移动到片段中,以便能够在同一个Activity中显示它们的内容,以便能够通过NavigationView在它们之间导航。 / p>

首先,当我返回包含FragmentStatePagerAdapter的Fragment时,适配器中的Fragments未显示。 我读了一些SO帖子,最后我发现我必须将ChildFragmentManager而不是SupportFragmentManager传递给FragmentStatePagerAdapter,因为它包含Fragment而不是Activity。

但在这种情况下我得到了这个错误,我无法找出问题所在。 如果我使用FragmentPagerAdapter而不是FragmentStatePagerAdapter它可以工作,但我想使用FragmentStatePagerAdapter。

我收到了这个错误:

  

10-12 12:36:30.022 25801-25801 / com.aff.index.main.debug E / AndroidRuntime:FATAL EXCEPTION:main                                                                             过程:com.aff.index.main.debug,PID:25801                                                                             java.lang.NullPointerException:尝试调用虚方法' java.lang.Object android.util.SparseArray.get(int)'在null对象引用上                                                                                 在android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:902)                                                                                 在android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:216)                                                                                 在android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1453)                                                                                 在android.view.View.dispatchRestoreInstanceState(View.java:15751)                                                                                 在android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3231)                                                                                 在android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3237)                                                                                 在android.view.View.restoreHierarchyState(View.java:15729)                                                                                 在android.support.v4.app.Fragment.restoreViewState(Fragment.java:510)                                                                                 在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1445)                                                                                 在android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)                                                                                 在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)                                                                                 在android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)                                                                                 在android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)                                                                                 在android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)                                                                                 在android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)                                                                                 在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)                                                                                 在android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:700)                                                                                 在android.os.Handler.handleCallback(Handler.java:751)                                                                                 在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                 在android.os.Looper.loop(Looper.java:154)                                                                                 在android.app.ActivityThread.main(ActivityThread.java:6119)                                                                                 at java.lang.reflect.Method.invoke(Native Method)                                                                                 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)                                                                                 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

0 个答案:

没有答案