当前位置:   article > 正文

java.lang.IllegalStateException: Fragment not attached to Activity异常的一个场景_fragment切换时 fragment{fae0dd2}} not attached to an

fragment切换时 fragment{fae0dd2}} not attached to an activity

有一个场景是在子线程中调用Fragment的getString()方法(如在Retrofit回调中、handler中等),而此时,有可能app被内存回收了,那么此时fragment已经detach了,就会发生这个异常。

解决方法:

1. 如果在Fragment里,并且是在子线程中调用getString()方法,则需要加一个isAdded()判断,这是一个Fragment类中的方法。

if(isAdded){

mTextView.setText(getString(R.string.something)); 

}

2. 尽可能使用application的context来getString()。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/253209
推荐阅读
相关标签
  

闽ICP备14008679号