java.lang.RuntimeException: Unable to start activity ComponentInfo{}java.lang.IllegalStateException: Activity has been destroyed
Caused by: java.lang.IllegalStateException: Activity has been destroyed
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1560)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:696)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:662)
fragment = new OneFragment(); mFragmentManager.beginTransaction() .add(com.handsomeyang.mango.R.id.root_relative_layout, fragment) //这里会报异常,activity has been destroyed .commit();
当我在Activity 的onCreate 中初始化fragment 并且用事务添加到Activity 的时候报错,说是Activity 已被销毁,然而怎么也不能理解,commit 源码点进去没找到,百度了好一会,在stackoverflow 上一个出现类似问题的评论中发现一句话: 说是Fragmentranscation 也是在onCrate中初始化的,也许是异步初始化,这样就涉及到代码执行顺序问题,最好把addFragment 往后提一提,我把这段同样的代码写在OnResume中就能顺利添加上fragment 并且不报错了。
希望能帮助到谁。