getChildFragmentManager

getChildFragmentManager

本文介绍了FragmentManager - IllegalStateException异常 - 无主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在面临很奇怪的问题,这个堆栈跟踪:

I'm facing very strange issue with this stacktrace:

Fatal Exception: java.lang.IllegalStateException No host
    android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1194)
    android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1189)
    android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated (FragmentManager.java:2001)
    android.support.v4.app.Fragment.performActivityCreated (Fragment.java:1976)
    android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1051)
    android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1207)
    android.support.v4.app.BackStackRecord.run (BackStackRecord.java:738)
    android.support.v4.app.FragmentManagerImpl.execPendingActions (FragmentManager.java:1572)
    android.support.v4.app.FragmentManagerImpl$1.run (FragmentManager.java:493)
    android.os.Handler.handleCallback (Handler.java:733)
    android.os.Handler.dispatchMessage (Handler.java:95)
    android.os.Looper.loop (Looper.java:146)
    android.app.ActivityThread.main (ActivityThread.java:5593)
    java.lang.reflect.Method.invokeNative (Method.java)
    java.lang.reflect.Method.invoke (Method.java:515)
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1283)
    com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1099)
    dalvik.system.NativeStart.main (NativeStart.java)

我跟踪多个版本的Andr​​oid(4.1.2,4.2.2,6.0)4 OCCURENCES此崩溃。我使用O很多片段,这意味着我无法弄清楚哪儿异常。

I tracked 4 occurences of this crash on multiple Android versions (4.1.2, 4.2.2, 6.0). I use o lot of Fragments, which means I can't figure out where exactly exception is thrown.

code FragmentManager

void moveToState(int newState, int transit, int transitStyle, boolean always) {
    if (mHost == null && newState != Fragment.INITIALIZING) {
        throw new IllegalStateException("No host");
    }
    ...
}

在我看来,这种情况发生时,有没有主机活动属于片段。我读过当 setRetainInstance()`调用此异常有时抛出的,但我根本不使用它。

In my opinion, this happens when there is no host Activity belonging to Fragment. Ive read this exception is sometimes thrown whensetRetainInstance()` is called, but I don't use it at all.

感谢每一个建议。

推荐答案

如果您有嵌套片段(延伸 FragmentActivity ),然后使用 getChildFragmentManager()而不是 getFragmentManager()

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

您也可以使用 getSupportFragmentManager()

编辑:
如果该片段是另一个片段的孩子,FragmentManager返回这里将成为母公司的。

注意:
返回此片段的宿主对象。如果片段没有被托管的可能返回null。看到

这篇关于FragmentManager - IllegalStateException异常 - 无主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 16:29