java.lang.ArrayIndexOutOfBoundsException: length=2; index=2 异常处理

  最近在做项目时,需求上需要ListView 有两个不同的布局,由于自己定义getItemViewType 的返回值,出现一个异常, 


 java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime:     at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:6647)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime:     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4998)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime:     at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4595)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:769)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime:     at android.view.Choreographer.doCallbacks(Choreographer.java:582)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime:     at android.view.Choreographer.doFrame(Choreographer.java:551)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:755)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5298)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:910)
09-05 15:15:42.166 21050-21050/com.julemai.julemai E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:705)


让我绞尽脑汁, 后来看了下Adapter源码发现以下代码:


/**
     * Get the type of View that will be created by {@link #getView} for the specified item.
     * 
     * @param position The position of the item within the adapter's data set whose view type we
     *        want.
     * @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
     *        <span style="background-color: rgb(51, 255, 51);"> range 0 to {@link #getViewTypeCount} - 1.</span> {@link #IGNORE_ITEM_VIEW_TYPE} can<pre name="code" class="java">
int getItemViewType(int position);
 

  意思是  getItemVIewType这个方法返回的整型范围: 0 to  (getViewTypeCount() -1)

getItemViewType 的返回值不能随便定义:

如下是我项目中的定义:

  @Override
    public int getItemViewType(int position) {
        AppLog.Log("firstData_view_type:" + lists.get(position).getCategory());
        int category = lists.get(position).getCategory();
        if (category == 1) {
            return 0;
        }
        return 1;
    }


    @Override
    public int getViewTypeCount() {
        return 2;
    }

写这篇文章,一个是为了让自己不再犯错, 二是给广大安卓开发者有遇到如似问题,可以快速解决。






  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值