我遇到了一个令人沮丧的问题。我有一个列表,它有一个适配器。我的适配器有一个 ArrayList,它包含对 ImageViews 的引用。
我的一个片段具有让用户从设备目录中选择文件的功能。onActivityResult 然后根据索引将返回的位图的缩略图设置为 imageView。因此,我从片段中调用了类似 adapter.getImageView(pos).setBitmap(bm) 的东西。
问题:当用户浏览照片库一段时间并最终选择时,我的应用程序崩溃并出现错误无法恢复活动,因为索引超出了我的 onActivityResult 的范围,因为它调用 getImageView(pos)
我认为正在发生的事情是,当用户浏览时间过长时,操作系统会回收我的应用程序并且它会被破坏,然后当用户回来时它会尝试重新创建它。奇怪的是我的适配器不是 Null,只是 ArrayList 未初始化。另一个奇怪的事情是我尝试调试并将 Log.v 放入该片段的 onResume() 和 onActivityResult 中。在这两种情况下,它似乎甚至在崩溃之前都没有进入这些方法。我很困惑......请帮忙。
这是完整的例外
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)