1

I am getting a frustrating issue. I have a list, and it has an adapter. My adapter has an ArrayList, which holds references to ImageViews.

One of my fragments has a function for user to go and select a file from a directory of the device. onActivityResult then sets a thumbnail of that returned bitmap to an imageView depending on index. So from fragment I am calling something like adapter.getImageView(pos).setBitmap(bm).

Problem: When a user goes and browses the photo gallery for a bit of time and finally selects , my app crashes with the error Could not resume activity, because index out of bounds from my onActivityResult, since it calls getImageView(pos)

I think what is happening is that when a user browses for too long, the OS recycles my app and it gets destroyed and then by the time user comes back it tries to recreate it. Strange is that my adapter is not Null, just ArrayList uninitialized. Another weird thing is I tried debugging and putting Log.v in the onResume() of that fragment and inside onActivityResult. In both cases it seems to not even get into those methods before it crashes. I am baffled... Please help.

Here is full exception

java.lang.RuntimeException: Unable to resume activity {com.core.somepackage/com.core.somepackage.SomeActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=67536, result=-1, data=Intent { dat=content://media/external/images/media/33276 flg=0x1 }} to activity {com.core.somepackage/com.core.somepackage.SomeActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2742)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2771)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2235)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5039)
            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:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=67536, result=-1, data=Intent { dat=content://media/external/images/media/33276 flg=0x1 }} to activity {com.core.somepackage/com.core.somepackage.SomeActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2729)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2771)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2235)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5039)
            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:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
            at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
            at java.util.ArrayList.get(ArrayList.java:304)
            at com.core.local.utils.adapters.UploadRecyclerViewAdapter.getImageView(UploadRecyclerViewAdapter.java:108)
            at com.core.main.UploadImageScreen.onActivityResult(UploadImageScreen.java:246)
            at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:165)
            at android.app.Activity.dispatchActivityResult(Activity.java:5293)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2729)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2771)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2235)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5039)
            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:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
vankiz
  • 106
  • 1
  • 6
  • The error meant your arraylist in that point has no values. but in some point of your application, you are calling one of its values with an index. What happens if you browse for short time>? – Sheychan Jul 13 '15 at 03:53
  • If browse for short time everything is fine. This happeba randomly when I think OS frees memorry – vankiz Jul 13 '15 at 03:54

1 Answers1

0

I have met similar question before, you can try the onRestoreInstanceState and onSaveInstanceState method.In the onSaveInstanceState, you can save your data; in the onSaveInstanceState method, you can get your data you have saved before.

protected void onSaveInstanceState(Bundle outState) {  
    outState.putInt("param", param);  
    super.onSaveInstanceState(outState);  
}  

protected void onRestoreInstanceState(Bundle savedInstanceState) {  
    param = savedInstanceState.getInt("param");   
    super.onRestoreInstanceState(savedInstanceState);  
}  

these are two method above.In my test, onRestoreInstanceState is invoked before onresume method.You can try these way and i hope this will be helpful.

LinaInverce
  • 166
  • 3
  • 13
  • Thanks for suggestion. I had plans to create restore instance state. Did not expect this to happen though. Will try that out tomorrow. Will post here after doing that. – vankiz Jul 13 '15 at 03:56
  • @vankiz did this answer fix your problem? – Sufian Aug 29 '16 at 07:34