My Android App has IndexOutOfBoundsException, how to solve?

隐身守侯 提交于 2020-01-17 14:05:11

问题


My Android app crash reporting service has reported many instances of:

java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:164)
at android.widget.AbsListView.onInitializeAccessibilityNodeInfoForItem(AbsListView.java:2410)
at android.widget.ListView.onInitializeAccessibilityNodeInfoForItem(ListView.java:3815)
at android.widget.AbsListView$ListItemAccessibilityDelegate.onInitializeAccessibilityNodeInfo(AbsListView.java:2341)
at android.view.View.onInitializeAccessibilityNodeInfo(View.java:5190)
at android.widget.LinearLayout.onInitializeAccessibilityNodeInfo(LinearLayout.java:1784)
at android.view.View.createAccessibilityNodeInfoInternal(View.java:5153)
at android.view.View$AccessibilityDelegate.createAccessibilityNodeInfo(View.java:19459)
at android.widget.AbsListView$ListItemAccessibilityDelegate.createAccessibilityNodeInfo(AbsListView.java:2333)
at android.view.View.createAccessibilityNodeInfo(View.java:5138)
at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:811)
at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:834)
at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:834)
at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchAccessibilityNodeInfos(AccessibilityInteractionController.java:720)
at android.view.AccessibilityInteractionController.findAccessibilityNodeInfoByAccessibilityIdUiThread(AccessibilityInteractionController.java:147)
at android.view.AccessibilityInteractionController.access$300(AccessibilityInteractionController.java:49)
at android.view.AccessibilityInteractionController$PrivateHandler.handleMessage(AccessibilityInteractionController.java:971)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5095)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)

I don't know what's causing this, I guess I will open a separate more focused question


回答1:


Your exception itself tells answer.You are trying to access element 1 which will be second element of array and array size is 1. Try to check size of array before accessing




回答2:


Access element at 0th index because your array size is 1 means it contain single element at 0th index position

java.lang.IndexOutOfBoundsException is thrown because your array contain only one element. It's at location index 0 not at 1. you are accessing element at index 1 which is not possible.

Try to access 0 index only don't go for more than 0.



来源:https://stackoverflow.com/questions/34587762/my-android-app-has-indexoutofboundsexception-how-to-solve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!