FragmentManager IllegalStateException: No host

UPDATE : Crashes gone when i put nullcheks (fragment != null, getActivity() != null) whereever i could.

My app is using ViewPager (offscreen limit 5) with FragmentStatePagerAdapter initialized with getSupportFragmentManager() . Each page has it's own fragments and backstack.

App crashes on startup. Problem occurs only on preLollipop devices and emulator. Using latest support lbrary (tried 24.0.0 also - same result) Stacktrace:

java.lang.IllegalStateException: No host at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1273) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1268) at 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.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) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 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.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719) at 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) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 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) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 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 com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291) at android.view.View.measure(View.java:16497) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java: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) at android.os.Looper.loop(Looper.java:136)


If you have nested Fragment (extends FragmentActivity), then use getChildFragmentManager() instead of getFragmentManager().

may be it will help you ..


In my case, I am using viewpager inside fragment and was using getChildFragmentManager() while setting viewpager adapter. It is also recommended to use this I guess.

There is also an issue, java.lang.IllegalStateException: No host exception was not showing everytime but it was showing too often when I called a method of viewpager containing Activity or coming back from another Activity class before setting viewpager.setCurrentItem(POSITION);

I tried in many ways and finally I changed getChildFragmentManager() to getActivity().getSupportFragmentManager();

After changing this java.lang.IllegalStateException: No host exception is not showing any more.

I think its related to Activity state issue which contains viewpager.


When you are using viewPager , make sure you are following these practices

  • call super(fragmentManager) in your constructor
  • if you are using Viewpager inside a Fragment, pass getChildFragmentManager in to Adapter
  • 链接地址: http://www.djcxy.com/p/84730.html

    上一篇: 错误使类com.elyeproj.loaderviewlibrary.LoaderImageViewImageView膨胀

    下一篇: FragmentManager IllegalStateException:没有主机