Unable to destroy activity: java.lang.IllegalArgumentException: No view found for id () for fragment

风流意气都作罢 提交于 2019-12-10 17:11:32

问题


I have an activity YYYY (which inflates a fragment) that extends a base activity XXXX which in turn extends ActionBarActivity. I now call finish() in onCreate() method of XXXX (at the very top), based on some condition. But I get the exception below. Kindly help me out.

PS: I call return; after the finish(); so that the rest of the onCreate() is not executed.

07-26 16:46:26.902  14569-14569/E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to destroy activity {XXXXXXXXXXXXXX}: java.lang.IllegalArgumentException: No view found for id 0x7f0c01ec (XXXXXXXX) for fragment XXXXXXXXXXXXXX{2c85cf32 #2 id=0x7f0c01ec}
    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3758)
    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3776)
    at android.app.ActivityThread.access$1400(ActivityThread.java:148)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1346)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5312)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0c01ec (XXXXXXXXXXX) for fragment XXXXXXXXXXXXXXX{2c85cf32 #2 id=0x7f0c01ec}
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:945)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
    at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1963)
    at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:313)
    at android.support.v7.app.ActionBarActivity.onDestroy(ActionBarActivity.java:166)
    at XXXXXXXXXXXXXXXXXXX.onDestroy(XXXXXXXXXXXX.java:100)
    at android.app.Activity.performDestroy(Activity.java:6132)
    at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1163)
    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3745)
    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3776)
    at android.app.ActivityThread.access$1400(ActivityThread.java:148)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1346)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5312)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

回答1:


OK, I found out what the problem is. The first time around calling finish() works perfectly. The problem occurs when the application is killed by the system and is recreated. If it's just an activity that is getting recreated, calling finish() after setContentView() works fine.

But if the activity has some fragments inflated in it, then when this particular activity is killed, Android stores the states of the fragments also. But it does not save this in the FragmentManager but rather a different list. So when the activity is recreated, Android already knows what the fragments are. Hence we can't just simply destroy it before inflating the fragment. If we do that, we get the exception during onDestroy()

So the ideal place to call the finish() is not just after setContentView(), but also after the onCreateView() of the fragments. Thus I ended by calling finish() in the onResume() of the activity. Things work fine.

Though I have found out the solution, I can't seem to find how to clear the list of fragment history that Android maintains when activity is recreated. If I could clear that list, then I could call finish() in the onCreate() itself. This is something that I am still searching for.




回答2:


If you call finish() from onCreate() that doesnt mean that onStart and onResume are not getting called (activity is onFinishing actually). To fix your issue u should call isFinishing() at onStart and onResume and if its true then return from that point.




回答3:


Add finish() after setContentView()



来源:https://stackoverflow.com/questions/31636481/unable-to-destroy-activity-java-lang-illegalargumentexception-no-view-found-fo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!