在App处于后台一段时间后,在ViewPager.getChildDrawingOrder中,Android应用有时会崩溃

时间:2019-03-04 09:59:58

标签: android nullpointerexception background android-viewpager

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get(int)' on a null object reference
   at android.support.v4.view.ViewPager.getChildDrawingOrder(ViewPager.java:798)
   at android.view.ViewGroup.getAndVerifyPreorderedIndex(ViewGroup.java:1986)
   at android.view.ViewGroup.populateChildrenForAutofill(ViewGroup.java:3712)
   at android.view.ViewGroup.getChildrenForAutofill(ViewGroup.java:3698)
   at android.view.ViewGroup.dispatchProvideAutofillStructure(ViewGroup.java:3680)
   at android.view.ViewGroup.dispatchProvideAutofillStructure(ViewGroup.java:3686)
   at android.view.ViewGroup.dispatchProvideAutofillStructure(ViewGroup.java:3686)
   at android.view.ViewGroup.dispatchProvideAutofillStructure(ViewGroup.java:3686)
   at android.view.ViewGroup.dispatchProvideAutofillStructure(ViewGroup.java:3686)
   at android.app.assist.AssistStructure$WindowNode.<init>(AssistStructure.java:510)
   at android.app.assist.AssistStructure.<init>(AssistStructure.java:1912)
   at android.app.ActivityThread.handleRequestAssistContextExtras(ActivityThread.java:3175)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1913)
   at android.os.Handler.dispatchMessage(Handler.java:105)
   at android.os.Looper.loop(Looper.java:164)
   at android.app.ActivityThread.main(ActivityThread.java:6944)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我只是像这样覆盖活动的onDestroy方法:

@Override
public void onDestroy(){
    finish();
    super.onDestroy();
}