我遇到了令人沮丧的问题。 我有一个列表,它有一个适配器。 我的适配器有一个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)
答案 0 :(得分:0)
之前我遇到过类似的问题,你可以试试onRestoreInstanceState和onSaveInstanceState方法。在onSaveInstanceState中,你可以保存 你的数据;在onSaveInstanceState方法中,您可以获取之前保存的数据。
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("param", param);
super.onSaveInstanceState(outState);
}
protected void onRestoreInstanceState(Bundle savedInstanceState) {
param = savedInstanceState.getInt("param");
super.onRestoreInstanceState(savedInstanceState);
}
这些是上面的两个方法。在我的测试中,在onresume方法之前调用onRestoreInstanceState。你可以尝试这些方式,我希望这会有所帮助。