gpt4 book ai didi

java - 无法恢复 Activity IndexOutOfBoundsException : Invalid index 0, 大小为 0

转载 作者:太空宇宙 更新时间:2023-11-04 13:39:18 27 4
gpt4 key购买 nike

我遇到了一个令人沮丧的问题。我有一个 list ,它有一个适配器。我的适配器有一个 ArrayList,它保存对 ImageView 的引用。

我的一个 fragment 具有让用户从设备目录中选择文件的功能。然后 onActivityResult 根据索引将返回的位图的缩略图设置为 imageView。因此,我从 fragment 中调用类似的内容:adapter.getImageView(pos).setBitmap(bm)。

问题:当用户浏览照片库一段时间并最终选择 时,我的应用程序崩溃并出现错误“无法恢复 Activity ,因为索引超出了我的 onActivityResult 的范围,因为它调用了 getImageView(pos)”

我认为正在发生的事情是,当用户浏览时间过长时,操作系统会回收我的应用程序并被破坏,然后当用户回来时它会尝试重新创建它。奇怪的是我的适配器不是Null,只是ArrayList未初始化。另一个奇怪的事情是我尝试调试并将 Log.v 放入该 fragment 的 onResume() 和 onActivityResult 中。在这两种情况下,它似乎在崩溃之前都没有进入这些方法。我很困惑...请帮忙。

这是完整的异常(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)

最佳答案

我以前也遇到过类似的问题,你可以尝试一下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);
}

这是上面的两个方法。在我的测试中,onRestoreInstanceState 是在 onresume 方法之前调用的。您可以尝试这些方法,希望对您有所帮助。

关于java - 无法恢复 Activity IndexOutOfBoundsException : Invalid index 0, 大小为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31375035/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com