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)
Fragment
是attach在Activity
上的,crash的原因顯然是你在嘗試調用notifyDataSetChanged
更新fragment時,該fragment的attached activity已經被銷毀了:“IllegalStateException: Activity has been destroyed”.一個workaround是這樣的: