listView中多个item布局时ArrayIndexOutOfBoundsException异常

最近在写ListView的多个item布局的时候产生了一个数组下标越界的异常,异常如下:

java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
    at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:6865)
    at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5220)
    at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3289)
    at android.widget.AbsListView.onTouchMove(AbsListView.java:3660)
    at android.widget.AbsListView.onTouchEvent(AbsListView.java:3499)
    at android.view.View.dispatchTouchEvent(View.java:7723)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2212)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2125)
    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1572)
    at android.app.Activity.dispatchTouchEvent(Activity.java:2535)
    at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:67)
    at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:67)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2073)
    at android.view.View.dispatchPointerEvent(View.java:7903)
    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4257)
    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4136)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3638)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3688)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3657)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3764)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3665)
    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3821)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3638)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3688)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3657)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3665)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3638)
    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5905)
    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5885)
    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5856)
    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5994)
    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
    at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
    at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
    at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:5958)
    at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:6017)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:780)
    at android.view.Choreographer.doCallbacks(Choreographer.java:593)
    at android.view.Choreographer.doFrame(Choreographer.java:560)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:766)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5315)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:680)
    at dalvik.system.NativeStart.main(Native Method)

十分的莫名其妙,经过各种百度谷歌以后发现,发现原来是getViewTypeCount和getItemViewType的问题。
getItemViewType返回的是item的类型,为我们定义的类型常量,而getViewTypeCount返回的是类型的种类数量,需要注意的是,getViewTypeCount返回值一定要大于我们定义的类型常量值,很是不理解为啥,不过确实解决了问题。不再崩溃了。
原先我是这么写的:

private static final int IS_END=1;
private static final int IS_NO_END=2;
private static final int IS_EQ=3;

@Overridepublic int getItemViewType(int position) {
    if (dates.isEmpty()){
        return 0;
    }
    FuceDate date=dates.get(position);
    int compare=DateUtil.getInstance(DateUtil.yyyy_MM_dd).compare(date.getMeasureDate(),
            DateUtil.getInstance(DateUtil.yyyy_MM_dd).getCurrentDate());
    return compare==-1?IS_END:compare==0?IS_EQ:IS_NO_END;
}
@Overridepublic int getViewTypeCount() {
    return 3;
}

这个么写就崩溃了 因为有一个类型的常量值是和类型总数相同的。必须要小于。修改为:

private static final int IS_END=0;
private static final int IS_NO_END=1;
private static final int IS_EQ=2;

如此完事大吉。不过并不是很清楚为啥,感觉有点莫名其妙。希望有路过的大神能留言解释一下,万分感谢。

你可能感兴趣的:(listView中多个item布局时ArrayIndexOutOfBoundsException异常)