Save Exception

I get the following exception with my application. I believe this happens when the user leaves the application for a long time, and then returns to it. I cannot reproduce it, but it often appears in my crash logs. I have no idea why this is happening, so any suggestions are welcome.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.MyActivity}: java.lang.ArrayIndexOutOfBoundsException: length=3; index=-1 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2237) at android.app.ActivityThread.access$600(ActivityThread.java:139) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:4974) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; index=-1 at java.util.ArrayList.get(ArrayList.java:306) at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1764) at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:198) at com.myapp.MyActivity.onCreate(MyActivity.java:235) at android.app.Activity.performCreate(Activity.java:4538) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2158) ... 11 more 

Line 235 is as follows:

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // line 235 

My onSaveInstanceState () is pretty simple:

 @Override public void onSaveInstanceState(Bundle state) { super.onSaveInstanceState(state); state.putInt("id1", id1); state.putInt("id2", id2); state.putInt("id3", id3); state.putInt("id4", id4); } 
+7
source share
3 answers

I ran into the same problem, I believe this is a bug with Android. Take a look at this topic for a possible solution http://code.google.com/p/android/issues/detail?id=22404

+4
source

I got the same problem, I followed the advice provided in Kenny's link:

I used the following:

 FragmentManager.enableDebugLogging(true); 

All calls to the FragmentManager will be displayed in the logs (fragments added, deleted, hidden, etc.).

This allowed me to see what was wrong in my case, one of the fragments in my application was sometimes not correctly stored in the instance and its contents were zero.

Then a crash occurred with the same error log when the application tried to restore this null fragment from the instance.

In my case, this fragment was a little special and was created automatically. In order not to get the error again, I deleted the fragment from the FragmentManager before exiting the Activity .

+2
source

I had the same problem with orientation changes, but this only happened with older versions of Android (4.0.4), newer versions like 4.4.2 were fine. I want to share with you how I fixed it.

As suggested by Kenny and Joanne, I used

 FragmentManager.enableDebugLogging(true); 

and found out that too much removal has been done. I had to remove ft.remove(df) from my code, and now it is good in both versions mentioned above. This is my code:

 if (savedInstanceState != null) { final FragmentTransaction ft = getFragmentManager().beginTransaction(); final DialogFragment df = (DialogFragment) getFragmentManager().findFragmentByTag("tag"); if (df != null) { df.dismiss(); // ft.remove(df); frag = new MyDialogFragment(); frag.show(getFragmentManager(), "tag"); } ft.addToBackStack(null); ft.commit(); } 

For completeness, this is what I did in onSaveInstanceState(final Bundle outstate) :

 if (frag != null) { getFragmentManager().putFragment(outState, "tag", frag); } 
0
source

All Articles