android - PagerAdapter.notifyDataSetChanged时崩溃:Activity has been destroyed
迷茫
迷茫 2017-04-17 14:31:27
0
1
601
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)
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回覆(1)
PHPzhong

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
// ...
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板