FragmentManager IllegalStateException:没有主机

更新 :崩溃时,我把nullcheks(片段!=空,getActivity()!=空)无论我可以。

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

应用程序在启动时崩溃。 问题只出现在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)在android.support.v4.app.Fragment.onCreate(Fragment.java:1254)在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)在android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1632)在android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:637)在安卓.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166)at android.support.v4.view.ViewPager.pop ulate(ViewPager.java:1237)at android.support.v4.view.ViewPager.populate(ViewPager.java:1085)at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1611)at 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)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)在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)at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)at 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 android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996) .view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5600)在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:761)在android.view.Choreographer.doCallbacks(Choreographer.java:574)的机器人。 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内部片段,并在设置viewpager适配器时使用getChildFragmentManager() 。 我也推荐使用这个。

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

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

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

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


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

  • 在你的构造函数中调用super(fragmentManager)
  • 如果您在Fragment中使用Viewpager,请将getChildFragmentManager传递给Adapter
  • 链接地址: http://www.djcxy.com/p/84729.html

    上一篇: FragmentManager IllegalStateException: No host

    下一篇: Alerdialog with Gridview(Imageadpater)