Android 索引越界错误,堆栈跟踪中没有应用程序代码
这是堆栈跟踪:
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)
我完全不知道如何重现此错误。我尝试随机单击应用程序上的每个 ListView
,但始终无法重现此错误。
这仅在应用程序投放市场后发生。我以前从未见过这个错误。
最糟糕的是,它没有来自我的应用程序的代码,所以我什至不知道发生这种情况时用户在做什么。
有人见过这种错误吗?
有谁知道从哪里开始解决这个问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我在运行 Lollipop 的 Nexus 5 上也遇到了同样的异常。如果我执行以下操作,就会发生这种情况:
添加虚拟页脚并没有修复它,它只是将错误更改为
索引 1 无效,大小为 1
:) ...但是感谢 @AlexD,我知道了在哪里寻找解决方法。在我的例子中,在成功创建 ListView 后调用
list.setFooterDividersEnabled(false)
可以防止崩溃的发生。I also ran into this same exception on a Nexus 5 running Lollipop. It would occur if I:
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.当列表视图为空时,我有时也会遇到同样的错误。查看源代码后,我发现了崩溃的行:
如您所见,没有检查 mFooterViewInfos 是否为空,因此我解决此问题的方法只是添加一个虚拟页脚:
I've got the same error sometime when listview is empty. After looking at the source code I've found the line that crashes:
As you can see there's no check if mFooterViewInfos is empty, so my workaround for this issue is just adding a dummy footer:
如果您在三星手机上运行此程序,请查看此 StackOverflow 问答:
Froyo 上的 ListView 随机 IndexOutOfBoundsException
If you're running this on a Samsung phone, please have a look at this StackOverflow Q&A:
ListView random IndexOutOfBoundsException on Froyo
也许有点晚了,但我想我找到了解决方法。想法是在离开之前删除 headerView/footerView (取决于您使用的内容)
片段/活动:
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:
也许您的活动被终止然后重新创建(例如当屏幕方向更改时),因此您的数组被初始化为零元素,但程序的其他部分不知道这一点并期望该数组具有一些元素。
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.