Why do I get IllegalStateExceptions every time I run an Activity that uses support fragments?

I use the Android support library to use snippets in pre-cellular applications.

If I add a fragment inside the onCreate method of my activity, the activity will end with an IllegalStateException.

08-04 10:19:49.100: ERROR/AndroidRuntime(18501): FATAL EXCEPTION: main 08-04 10:19:49.100: ERROR/AndroidRuntime(18501): java.lang.RuntimeException: Unable to start activity ComponentInfo{net..../net.....homescreen...Main}: java.lang.IllegalStateException: Activity has been destroyed 08-04 10:19:49.100: ERROR/AndroidRuntime(18501): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 08-04 10:19:49.100: ERROR/AndroidRuntime(18501): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 08-04 10:19:49.100: ERROR/AndroidRuntime(18501): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 08-04 10:19:49.100: ERROR/AndroidRuntime(18501): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 08-04 10:19:49.100: ERROR/AndroidRuntime(18501): at android.os.Handler.dispatchMessage(Handler.java:99) 08-04 10:19:49.100: ERROR/AndroidRuntime(18501): at android.os.Looper.loop(Looper.java:123) 08-04 10:19:49.100: ERROR/AndroidRuntime(18501): at android.app.ActivityThread.main(ActivityThread.java:3691) 08-04 10:19:49.100: ERROR/AndroidRuntime(18501): at java.lang.reflect.Method.invokeNative(Native Method) 08-04 10:19:49.100: ERROR/AndroidRuntime(18501): at java.lang.reflect.Method.invoke(Method.java:507) 08-04 10:19:49.100: ERROR/AndroidRuntime(18501): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847) 08-04 10:19:49.100: ERROR/AndroidRuntime(18501): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 08-04 10:19:49.100: ERROR/AndroidRuntime(18501): at dalvik.system.NativeStart.main(Native Method) 08-04 10:19:49.100: ERROR/AndroidRuntime(18501): Caused by: java.lang.IllegalStateException: Activity has been destroyed 08-04 10:19:49.100: ERROR/AndroidRuntime(18501): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1257) 08-04 10:19:49.100: ERROR/AndroidRuntime(18501): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:535) 08-04 10:19:49.100: ERROR/AndroidRuntime(18501): at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:519) 08-04 10:19:49.100: ERROR/AndroidRuntime(18501): at net.....AbstractActivity.onCreate(AbstractActivity.java:103) 08-04 10:19:49.100: ERROR/AndroidRuntime(18501): at net.....homescreen...Main.onCreate(..Main.java:51) 08-04 10:19:49.100: ERROR/AndroidRuntime(18501): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 08-04 10:19:49.100: ERROR/AndroidRuntime(18501): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 08-04 10:19:49.100: ERROR/AndroidRuntime(18501): ... 11 more 

The onCreatate AbstractActivity value is as follows:

 protected void onCreate(Bundle savedInstanceState) { if (menuEnabled) { FragmentManager fragmentManager = getCompatibleFragmentManager(); FragmentTransaction transaction = fragmentManager .beginTransaction(); transaction.add(new OptionsMenuFragment(), OPTIONS_MENU_IDENTIFIER); transaction.commit(); } super.onCreate(savedInstanceState); } 

If I debug the call, I see that the instancemanager throws this exception because the action is null. It seems that the fragment handler is not initialized at all.

What am I doing wrong?

+6
source share
1 answer

It turns out that the FragmentActivity from the compatibility packages initializes the fragment manager in the onCreate method itself.

If you move the call to super at the beginning of my onCreate method, everything will be fine.

+13
source

Source: https://habr.com/ru/post/894257/


All Articles