美文网首页
IllegalStateException:"Frag

IllegalStateException:"Frag

作者: Avalon1 | 来源:发表于2018-10-31 17:03 被阅读20次

简单的说这个异常是因为,在Activity已经destroy,而此时Fragment中的某个方法调用到了context.

常见情况 在执行某个异步请求时候,Activity销毁,而请求没有被停止,回调中用到了context相关.

比如getResource()

而此方法的源码就有

if (mHost == null) {
    throw new IllegalStateException("Fragment " + this + " not attached to Activity");
}
return mHost.getContext().getResources();

所以必须在destroy的时候取消掉请求.可以通过 rxlifecycle 或者自己在生命周期中进行处理.

相关文章

网友评论

      本文标题:IllegalStateException:"Frag

      本文链接:https://www.haomeiwen.com/subject/fxnatqtx.html