Android:Java.lang.IllegalStateException Activity has been destroyed

在使用ViewPager中动态添加Fragment A,同时Fragment A中嵌套Fragment时,在不停滑动ViewPager时,会出现"Java.lang.IllegalStateException Activity has been destroyed"的错误,解决办法如下:

在Fragment A中添加:

@Override
public void onDetach() {
    super.onDetach();

    try {
        Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
        childFragmentManager.setAccessible(true);
        childFragmentManager.set(this, null);

    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

参考: http://stackoverflow.com/questions/15207305/getting-the-error-java-lang-illegalstateexception-activity-has-been-destroyed

你可能感兴趣的:(Exception,Android)