Java.lang.IllegalStateException Activity has been destroyed


03-04 12:01:05.468: E/AndroidRuntime(2474): FATAL EXCEPTION: main
java.lang.IllegalStateException: Activity has been destroyed 
    at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1345) 
    at android.app.BackStackRecord.commitInternal(BackStackRecord.java:597) 
    at android.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:579) 
    at android.support.v13.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143) 
    at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:892) 

bug出現的原理問題及解決方法是
This seems to be a bug in the newly added support for nested fragments. Basically, the child FragmentManager ends up with a broken internal state when it is detached from the activity. A short-term workaround that fixed it for me is to add the following to onDetach() of every Fragment which you call getChildFragmentManager() on:

解決方法重寫onDetach()

@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

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM