Android index out of bounds error with no application code in stack trace
This is the stack trace:
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257) at java.util.ArrayList.get(ArrayList.java:311) at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:164) at android.widget.ListView.dispatchDrawWithExcessScroll_Default(ListView.java:3354) at android.widget.ListView.dispatchDraw(ListView.java:3054) at android.view.View.draw(View.java:6936) at android.widget.AbsListView.draw(AbsListView.java:3022) at android.view.ViewGroup.drawChild(ViewGroup.java:1646) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) at android.view.View.draw(View.java:6936) at android.view.ViewGroup.drawChild(ViewGroup.java:1646) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) at android.view.ViewGroup.drawChild(ViewGroup.java:1644) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) at android.view.ViewGroup.drawChild(ViewGroup.java:1644) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) at android.view.ViewGroup.drawChild(ViewGroup.java:1644) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) at android.view.ViewGroup.drawChild(ViewGroup.java:1644) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) at android.view.ViewGroup.drawChild(ViewGroup.java:1644) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) at android.view.ViewGroup.drawChild(ViewGroup.java:1644) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) at android.view.ViewGroup.drawChild(ViewGroup.java:1644) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) at android.view.View.draw(View.java:6936) at android.view.ViewGroup.drawChild(ViewGroup.java:1646) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) at android.view.ViewGroup.drawChild(ViewGroup.java:1644) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) at android.view.View.draw(View.java:6936) at android.widget.FrameLayout.draw(FrameLayout.java:357) at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1904) at android.view.ViewRoot.draw(ViewRoot.java:1527) at android.view.ViewRoot.performTraversals(ViewRoot.java:1263) at android.view.ViewRoot.handleMessage(ViewRoot.java:1865) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:3687) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) at dalvik.system.NativeStart.main(Native Method)
I have absolutely no idea how to reproduce this error. I've tried randomly click at every ListView on my application and was never able to reproduce this error.
This only happened after the application was shipped to the market. I've never seen this error before.
The worst part is that it has no code from my application so I don't even know what the user was doing when this happened.
Did anyone ever see this kind of error?
Does anyone have a clue where to start fixing this?
Answers
If you're running this on a Samsung phone, please have a look at this StackOverflow Q&A:
ListView random IndexOutOfBoundsException on Froyo
I also ran into this same exception on a Nexus 5 running Lollipop. It would occur if I:
- unplugged the device while a list is shown
- let the device fall asleep
- plugged the device back in, causing the list contents to be refreshed
Adding a dummy footer didn't fix it, it just changed the error to Invalid index 1, size is 1 :) ...but thanks to @AlexD I had a clue of where to look for workarounds.
In my case calling list.setFooterDividersEnabled(false) after the ListView is created successfully prevents the crash from occurring.
I've got the same error sometime when listview is empty. After looking at the source code I've found the line that crashes:
return mFooterViewInfos.get(adjPosition - adapterCount).isSelectable
As you can see there's no check if mFooterViewInfos is empty, so my workaround for this issue is just adding a dummy footer:
list.addFooterView(new View(context, null, false));
Maybe a litle bit late but I think i found workaround for this. The think is to remove headerView/footerView (depending on what you use) before leaving fragment/activity:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { .... getListView().addHeaderView(mHeaderView); } public void onDestroyView() { super.onDestroyView(); if (getListView().getHeaderViewsCount() > 0) { getListView().removeHeaderView(mHeaderView); } }
Maybe your activity gets killed and then recreated (such as when screen orientation changes) so your array is being initialized to zero elements and yet some other part of your program don't know about that and expect that array to have some elements.