Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

android - FragmentManager - IllegalStateException - No Host

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)

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 of FragmentManager:

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

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.

Thanks for every advice.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

You can also use getSupportFragmentManager().

EDIT: If this Fragment is a child of another Fragment, the FragmentManager returned here will be the parent's getChildFragmentManager().

NOTE: Return the host object of this fragment. May return null if the fragment isn't currently being hosted. See http://developer.android.com/reference/android/app/Fragment.html#getHost()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...