ArrayAdapter and ListView - ArrayIndexOutOfBoundsException

Deadly 提交于 2019-12-06 02:58:43

问题


I'm really hoping someone can help me out with this one. I've been stuck on it forever. Occasionally, when someone is using my app, it'll force close with this exception:

java.lang.ArrayIndexOutOfBoundsException
    at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:3572)
    at android.widget.AbsListView.trackMotionScroll(AbsListView.java:2487)
    at android.widget.AbsListView.onTouchEvent(AbsListView.java:2001)
    at android.widget.ListView.onTouchEvent(ListView.java:3234)
    at android.view.View.dispatchTouchEvent(View.java:3709)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:874)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1695)
    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1116)
    at android.app.Activity.dispatchTouchEvent(Activity.java:2068)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1679)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1695)
    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1116)
    at android.app.Activity.dispatchTouchEvent(Activity.java:2068)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1679)
    at android.view.ViewRoot.handleMessage(ViewRoot.java:1697)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4568)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    at dalvik.system.NativeStart.main(Native Method)

However, I cannot figure out why. It's relatively rare, non-reproducable, and the stacktrace is completely unhelpful because it doesn't include any of my code.

Is it a bug in Android itself?


回答1:


What version of Android are you running? From what I can tell your Adapter is returning the wrong count from getViewTypeCount() (or you are changing the view type count dynamically which is a big mistake.)




回答2:


In addition to what Romain said, it seems to be important that the largest view type is smaller than the view type count.

I had two view types defined (for what ever reason) with 1 and 2 and returned 2 in getViewTypeCount() which gave me the same exception as above. Re-indexing the types to 0 and 1 fixed the issue.



来源:https://stackoverflow.com/questions/3017496/arrayadapter-and-listview-arrayindexoutofboundsexception

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