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

I have an activity YYYY

(which inflates a fragment) that expands the underlying activity XXXX

, which in turn expands ActionBarActivity

. Now I call finish()

into the onCreate()

method XXXX

(at the very top) based on some condition. But I am getting below exception. Please help me.

PS: I am calling return;

after finish();

so that the rest is onCreate()

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)

      

+3


source to share


3 answers


Ok I figured out what the problem is. finish()

Works fine the first time it is called . The problem occurs when the application is killed by the system and recreated. If it's just an action being recreated, the call finish()

after setContentView()

works fine.

But if there are some fragments in the activity bloated in it, then when that particular activity is killed, the states of the fragments are also stored in Android. But that doesn't store it in the FragmentManager, but rather in a different list. So when the activity is recreated, Android already knows what fragments are. Hence, we cannot just simply destroy it before inflating the fragment. If we do this, we will get an exception duringonDestroy()



Therefore, the ideal place to call finish()

occurs not only after setContentView()

but also after the onCreateView()

fragments. So I ended up typing finish()

in onResume()

activity. Everything works fine.

Although I have found a solution, I cannot find how to clear the Android maintained chunk history list when the activity is updated. If I could clear this list, then I could call finish()

in itself onCreate()

. This is what I am still looking for.

+3


source


If you call finish () from onCreate () it doesn't mean onStart and onResume don't get called (the activity actually ends). To fix your problem, you should call isFinishing () on onStart and onResume, and if true, then return from that point.



0


source


Add finish () after setContentView ()

0


source







All Articles