FragmentManager IllegalStateException:没有主机

时间:2016-08-22 15:51:00

标签: android android-layout android-fragments android-viewpager

更新: 当我把nullcheks(片段!= null,getActivity()!= null)放进去的时候,崩溃就消失了。

我的应用正在使用 ViewPager (屏幕外限制5),并使用 getSupportFragmentManager()初始化 FragmentStatePagerAdapter 。每个页面都有自己的片段和后台堆栈。

应用程序在启动时崩溃。问题仅出现在 preLollipop 设备和模拟器上。使用最新的支持lbrary(试过24.0.0也 - 同样的结果) 堆栈跟踪:

  

java.lang.IllegalStateException:没有主持人   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1273)                                                                              在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1268)                                                                              在   android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:2138)                                                                              在android.support.v4.app.Fragment.onCreate(Fragment.java:1254)                                                                              在android.support.v4.app.Fragment.performCreate(Fragment.java:2062)                                                                              在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1051)                                                                              在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)                                                                              在   android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)                                                                              在   android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1632)                                                                              在   android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:637)                                                                              在   android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166)                                                                              在android.support.v4.view.ViewPager.populate(ViewPager.java:1237)                                                                              在android.support.v4.view.ViewPager.populate(ViewPager.java:1085)                                                                              在android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1611)                                                                              在android.view.View.measure(View.java:16497)                                                                              在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)                                                                              在   android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)                                                                              在android.widget.LinearLayout.measureVertical(LinearLayout.java:695)                                                                              在android.widget.LinearLayout.onMeasure(LinearLayout.java:588)                                                                              在android.view.View.measure(View.java:16497)                                                                              在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)                                                                              在android.widget.FrameLayout.onMeasure(FrameLayout.java:310)                                                                              在android.view.View.measure(View.java:16497)                                                                              在   android.widget.RelativeLayout.measureChildHorizo​​ntal(RelativeLayout.java:719)                                                                              在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)                                                                              在android.view.View.measure(View.java:16497)                                                                              在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)                                                                              在android.widget.FrameLayout.onMeasure(FrameLayout.java:310)                                                                              在   android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)                                                                              在android.view.View.measure(View.java:16497)                                                                              在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)                                                                              在android.widget.FrameLayout.onMeasure(FrameLayout.java:310)                                                                              在android.view.View.measure(View.java:16497)                                                                              在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)                                                                              在android.widget.FrameLayout.onMeasure(FrameLayout.java:310)                                                                              在android.view.View.measure(View.java:16497)                                                                              在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)                                                                              在   android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)                                                                              在android.widget.LinearLayout.measureVertical(LinearLayout.java:695)                                                                              在android.widget.LinearLayout.onMeasure(LinearLayout.java:588)                                                                              在android.view.View.measure(View.java:16497)                                                                              在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)                                                                              在android.widget.FrameLayout.onMeasure(FrameLayout.java:310)                                                                              在   com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2291)                                                                              在android.view.View.measure(View.java:16497)                                                                              在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)                                                                              在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)                                                                              在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)                                                                              在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)                                                                              在   android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5600)                                                                              在   android.view.Choreographer $ CallbackRecord.run(Choreographer.java:761)                                                                              在android.view.Choreographer.doCallbacks(Choreographer.java:574)                                                                              在android.view.Choreographer.doFrame(Choreographer.java:544)                                                                              在   android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:747)                                                                              在android.os.Handler.handleCallback(Handler.java:733)                                                                              在android.os.Handler.dispatchMessage(Handler.java:95)                                                                              在android.os.Looper.loop(Looper.java:136)

3 个答案:

答案 0 :(得分:1)

如果你有嵌套的Fragment(扩展FragmentActivity),那么使用getChildFragmentManager()而不是getFragmentManager()。

可能会帮助你..

答案 1 :(得分:1)

就我而言,我在使用viewpager inside fragment并在设置viewpager适配器时使用getChildFragmentManager()。我猜也建议使用它。

还有一个问题,java.lang.IllegalStateException: No host异常没有每次都显示但是当我在设置viewpager.setCurrentItem(POSITION);之前调用包含Activity的viewpager方法或从另一个Activity类返回时,它常常显示p>

我尝试了多种方式,最后我将getChildFragmentManager()更改为getActivity().getSupportFragmentManager();

更改后,java.lang.IllegalStateException: No host异常不再显示。

我认为它与包含viewpager的活动状态问题有关。

答案 2 :(得分:0)

使用viewPager时,请确保遵循这些做法

  1. 在构造函数中调用super(fragmentManager)
  2. 如果您在片段中使用Viewpager,请将getChildFragmentManager传递给适配器