java.lang.IllegalStateException: No activity

3 篇文章 0 订阅
2 篇文章 0 订阅

错误提示:

java.lang.IllegalStateException: No activity
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1861)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1474)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)

.......

这个问题是在解决上一篇文章(http://blog.csdn.net/leewenjin/article/details/19409863)中指出的问题后出现的。问题解决方法是参考文章:

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

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()

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. @Override  
  2.     public void onDetach() {  
  3.         super.onDetach();  
  4.         try {  
  5.             Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");  
  6.             childFragmentManager.setAccessible(true);  
  7.             childFragmentManager.set(thisnull);  
  8.   
  9.         } catch (NoSuchFieldException e) {  
  10.             throw new RuntimeException(e);  
  11.         } catch (IllegalAccessException e) {  
  12.             throw new RuntimeException(e);  
  13.         }  
  14.       
  15.     }  

其中的 Field 是

Open Declaration java.lang.reflect.Field

引起bug的原因
If you look at the implementation of Fragment, you'll see that when moving to the detached state, it'll reset its internal state. However, it doesn't reset mChildFragmentManager (this is a bug in the current version of the support library). This causes it to not reattach the child fragment manager when the Fragment is reattached, causing the exception you saw.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值