Android Listview 中出现 at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:7272) 错误

来源:互联网 发布:知行理工忘记密码 编辑:程序博客网 时间:2024/06/05 10:19
E/AndroidRuntime( 9327): java.lang.ArrayIndexOutOfBoundsException: length=2; index=2E/AndroidRuntime( 9327):        at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:7272)E/AndroidRuntime( 9327):        at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5596)E/AndroidRuntime( 9327):        at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:5133)E/AndroidRuntime( 9327):        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:804)E/AndroidRuntime( 9327):        at android.view.Choreographer.doCallbacks(Choreographer.java:607)E/AndroidRuntime( 9327):        at android.view.Choreographer.doFrame(Choreographer.java:575)E/AndroidRuntime( 9327):        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:790)E/AndroidRuntime( 9327):        at android.os.Handler.handleCallback(Handler.java:815)E/AndroidRuntime( 9327):        at android.os.Handler.dispatchMessage(Handler.java:104)E/AndroidRuntime( 9327):        at android.os.Looper.loop(Looper.java:224)E/AndroidRuntime( 9327):        at android.app.ActivityThread.main(ActivityThread.java:5911)E/AndroidRuntime( 9327):        at java.lang.reflect.Method.invoke(Native Method)E/AndroidRuntime( 9327):        at java.lang.reflect.Method.invoke(Method.java:372)E/AndroidRuntime( 9327):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1113)E/AndroidRuntime( 9327):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879)

错误原因:
StackOverflow 中解释:“The item view type you are returning from getItemViewType() is >= getViewTypeCount().”;中文意思就是 getViewTypeCount() 的值必须大于 getItemViewType() 的值,反之,则会出现标题中的错误.
ListView包含多个item条目的时候,我们需要在Adapter适配器中重写 getItemViewType() 和 getViewTypeCoun() 两个方法,我们通常在 getItemViewType() 中给具体的item做区分(这两个方法的返回值类型都为 int 类型),以便在 getView() 加载不同的 layout 布局,而在 getViewTypeCount() 方法中定义一共有多少种不同的item,导致标题中的错误出现的原因,
getItemViewType() 源码中的注释也提醒我们:

* @return An integer representing the type of View. Two views should share the same type if one*         can be converted to the other in {@link #getView}. Note: Integers must be in the*         range 0 to {@link #getViewTypeCount} - 1. {@link #IGNORE_ITEM_VIEW_TYPE} can*         also be returned.

通常我们把 Type 值设置为从0开始计数即可保证最大值的 Type 类型也不会大于 getViewTypeCount() 方法的返回值,

0 0