恢复活动

时间:2017-02-14 05:58:25

标签: android android-fragments fragment fragmentmanager

问题是什么: 我有一个主要活动,它在加载时总共添加了7个片段:2个抽屉导航片段,1个viewPager主片段和4个viewpager子片段。

只要有需要,就会添加

新片段。因此在问题发生之前会添加两个碎片。并且添加的最后一个片段打开另一个活动“cardActivity”。当此cardActivity以编程方式按下后完成时,它将完成,然后发生以下错误:

02-14 11:06:29.759 16930-16930/com.myapp.app E/UncaughtException: java.lang.RuntimeException: Unable to resume activity {com.myapp.app/com.myapp.app.Activities.MainActivity}: java.lang.IndexOutOfBoundsException: Invalid index 7, size is 7
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2800)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2829)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1245)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5118)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 7, size is 7
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1584)
at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:2842)
at android.support.v4.app.FragmentController.dispatchResume(FragmentController.java:223)
at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:509)
at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:498)
at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:175)
at android.app.Activity.performResume(Activity.java:5367)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2790)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2829) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1245) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5118) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 
at dalvik.system.NativeStart.main(Native Method) 
02-14 11:06:30.062 16930-16930/com.myapp.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp.app, PID: 16930
java.lang.RuntimeException: Unable to resume activity {com.myapp.app/com.myapp.app.Activities.MainActivity}: java.lang.IndexOutOfBoundsException: Invalid index 7, size is 7
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2800)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2829)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1245)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5118)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 7, size is 7
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1584)
at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:2842)
at android.support.v4.app.FragmentController.dispatchResume(FragmentController.java:223)
at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:509)
at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:498)
at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:175)
at android.app.Activity.performResume(Activity.java:5367)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2790)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2829) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1245) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5118) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 
at dalvik.system.NativeStart.main(Native Method) 

此错误随机发生,有时会发生,有时也不会发生。

到目前为止我尝试了什么: 我试图重现这个问题,并检查是否有一些片段被删除,但没有删除片段。

我在此处也发现了同样的问题:https://code.google.com/p/android/issues/detail?id=231430 但是那里也没有解决方案。 此外,在MainActivity的OnResume()中没有做任何事情,它刚刚被覆盖,没有别的:

    @Override
protected void onResume() {
    super.onResume();
}

有谁能告诉我这个问题的可能原因是什么?

2 个答案:

答案 0 :(得分:2)

尝试使用此处理异常

 @Override
protected void onPostResume() {
    try {
        super.onPostResume();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

答案 1 :(得分:1)

这不是解决方案,而是朝着正确方向迈出的一步:)

我想这就是问题 - https://code.google.com/p/android/issues/detail?id=231430

有人已经报告了此情况,目前正在跟踪。我认为您可以添加2美分,并帮助您了解此问题何时发生,如果可能,请提供您的应用程序进行抽样。