PagerAdapter.notifyDataSetChanged时崩溃:Activity has been destroyed

薄炳鑫
  • 168
10-20 12:05:52.392 30367-30367/com.dreamiii.enviroment E/AndroidRuntime: java.lang.IllegalStateException: Activity has been destroyed
10-20 12:05:52.392 30367-30367/com.dreamiii.enviroment E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1522)
10-20 12:05:52.392 30367-30367/com.dreamiii.enviroment E/AndroidRuntime:     at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:634)
10-20 12:05:52.392 30367-30367/com.dreamiii.enviroment E/AndroidRuntime:     at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:617)
10-20 12:05:52.392 30367-30367/com.dreamiii.enviroment E/AndroidRuntime:     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:139)
10-20 12:05:52.392 30367-30367/com.dreamiii.enviroment E/AndroidRuntime:     at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:930)
10-20 12:05:52.392 30367-30367/com.dreamiii.enviroment E/AndroidRuntime:     at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2910)
10-20 12:05:52.392 30367-30367/com.dreamiii.enviroment E/AndroidRuntime:     at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
10-20 12:05:52.392 30367-30367/com.dreamiii.enviroment E/AndroidRuntime:     at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
10-20 12:05:52.392 30367-30367/com.dreamiii.enviroment E/AndroidRuntime:     at com.dreamiii.enviroment.MainActivity$1$1.run(MainActivity.java:31)
10-20 12:05:52.392 30367-30367/com.dreamiii.enviroment E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:733)
10-20 12:05:52.392 30367-30367/com.dreamiii.enviroment E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
10-20 12:05:52.392 30367-30367/com.dreamiii.enviroment E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136)
10-20 12:05:52.392 30367-30367/com.dreamiii.enviroment E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5001)
10-20 12:05:52.392 30367-30367/com.dreamiii.enviroment E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
10-20 12:05:52.392 30367-30367/com.dreamiii.enviroment E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
10-20 12:05:52.392 30367-30367/com.dreamiii.enviroment E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-20 12:05:52.392 30367-30367/com.dreamiii.enviroment E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-20 12:05:52.392 30367-30367/com.dreamiii.enviroment E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
回复
阅读 4.8k
1 个回答
li21
  • 3.1k

Fragment是attach在Activity上的,crash的原因显然是你在尝试调用notifyDataSetChanged更新fragment时,该fragment的attached activity已经被销毁了:“IllegalStateException: Activity has been destroyed”.
一个workaround是这样的:

// here you check the value of getActivity() and break up if needed
if(getActivity() == null) {
    return;
}
// do your stuff to update fragment
// ...
宣传栏