FragmentManager IllegalStateException:没有主机

更新 :当我把nullcheks(片段!= null,getActivity()!= null)崩溃了,无论我能。

我的应用程序正在使用ViewPager (屏幕外限制5)与FragmentStatePagerAdaptergetSupportFragmentManager()初始化。 每个页面都有它自己的碎片和后台堆栈。

应用程序在启动时崩溃。 问题只出现在preLollipop设备和模拟器上。 使用最新的支持lbrary(也尝试24.0.0 – 相同的结果)Stacktrace:

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)at android.support.v4.app.Fragment.onCreate(Fragment.java:1254)at android.support.v4.app.Fragment.performCreate(Fragment.java :2062)at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1051)at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)at android.support.v4.app。 BackStackRecord.run(BackStackRecord.java:758)at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1632)at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:637)at android在android.support.v4.view.ViewPager.pop上的.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166) ulate(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)at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)at android.widget.LinearLayout.measureVertical( LinearLayout.java:695)在android.view.View.measure(View.java:16497)android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)android.widget.LinearLayout.onMeasure(LinearLayout.java:588) )android.widget.FrameLayout.onMeasure(FrameLayout.java:310)在android.widget android.widget.RelativeLayout.measureChildHorizo​​ntal(RelativeLayout.java:719)android.view.View.measure(View.java:16497) .RelativeLayout.onMeasure(RelativeLayout.java:455)at android.view.View.measure(View.java:16497)at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)at android.widge t.FrameLayout.onMeasure(FrameLayout.java:310)at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)at android.view.View.measure(View.java:16497)at android.view .ViewGroup.measureChildWithMargins(ViewGroup.java:5125)at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)at android.view.View.measure(View.java:16497)at android.view.ViewGroup.measureChildWithMargins( (ViewGroup.java:5125)在android.view.View.measure(View.java:16497)android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)android.widget.FrameLayout.onMeasure(FrameLayout.java:310) )在android.widget.Lineout.measureChildChildChildChildChildChildChildVertical(LinearLayout.java:695)在android.widget.LinearLayout.onMeasure(LinearLayout.java:588)在android.view上的(LinearLayout.java:1404)在android.widget.LinearLayout.measureVertical .View.measure(View.java:16497)at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 在android.view.View.measure(View.java:16497)在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java)com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2291) :1912)at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)at android .view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5600)at android.view.Choreographer $ CallbackRecord.run(Choreographer.java:761)at android.view.Choreographer.doCallbacks(Choreographer.java:574)at android。 view.Choreographer.doFrame(Choreographer.java:544)at android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:747)at android.os.Handler.handleCallback(Handler.java:733)at android.os.Handler .dispatchMessage(Handler.java:95)在android.os.Looper.loop(Looper.java:136)

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

可能会帮助你..

在我的情况下,我使用viewpager里面的片段,并使用getChildFragmentManager()同时设置viewpager适配器。 我也推荐使用这个。

还有一个问题, java.lang.IllegalStateException: No hostexception没有显示每一次,但它显示过于频繁,当我调用viewpager包含活动的方法或返回从另一个Activity类之前设置viewpager.setCurrentItem(POSITION);

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

更改此java.lang.IllegalStateException: No hostexception不再显示。

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

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

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