Android 关于多点触摸抛出ArrayIndexOutOfBoundsException异常问题的解决

今天在搞项目的时候突然发现其中一个模块(柱状图图表)频繁出现程序崩溃问题,之所以说是频繁,是因为出现崩溃问题具有一定的偶然性,一根手指左右滑动图表,没有任何问题,两根手指对图表进行缩放,也没有问题,一切看似全都正常,那么好了,我用两根手指对图表反复快速的进行缩放操作,问题就很容易出现了。

下面先看一下异常信息:

E/MessageQueue-JNI: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
	 at android.support.v4.widget.ViewDragHelper.shouldInterceptTouchEvent(ViewDragHelper.java:1014)
	 at android.support.v4.widget.DrawerLayout.onInterceptTouchEvent(DrawerLayout.java:1282)
	 at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2313)
	 at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2712)
	 at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2428)
	 at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2712)
	 at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2428)
	 at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2712)
	 at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2428)
	 at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2712)
	 at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2428)
	 at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2712)
	 at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2428)
	 at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2679)
	 at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1797)
	 at android.app.Activity.dispatchTouchEvent(Activity.java:2878)
	 at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
	 at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
	 at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2640)
	 at android.view.View.dispatchPointerEvent(View.java:9234)
	 at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4785)
	 at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4623)
	 at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4174)
	 at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4227)
	 at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4193)
	 at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4303)
	 at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4201)
	 at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4360)
	 at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4174)
	 at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4227)
	 at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4193)
	 at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4201)
	 at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4174)
	 at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6652)
	 at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6536)
	 at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6507)
	 at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6742)
	 at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
	 at android.os.MessageQueue.nativePollOnce(Native Method)
	 at android.os.MessageQueue.next(MessageQueue.java:143)
	 at android.os.Looper.loop(Looper.java:130)
	 at android.app.ActivityThread.main(ActivityThread.java:6117)
	 at java.lang.reflect.Method.invoke(Native Method)
	 at java.lang.reflect.Method.invoke(Method.java:372)
	 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
	 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
没错,数组越界异常,再接着往下看,看到异常指向了V4包中DrawerLayout的  onInterceptTouchEvent。通过异常信息定位到代码中一通乱找,然而并没有什么卵用。

这个问题是关于多点触摸的requestDisallowInterceptTouchEvent和DrawerLayout的innerViews问题,通过百度,在这里找到了解决方法。

因为当innerViews做requestDisallowInterceptTouchEvent(true),DrawerLayout的onInterceptTouchEvent将不会被调用。 因此,在下面的情况下,它会抛出

ArrayIndexOutOfBoundsException异常。

用户触摸屏幕(pointerId=0),并触发做requestDisallowInterceptTouchEvent(true)。 然后,用户将另一个手指(pointerId= 1)在屏幕上在那个时候(当

requestDisallowInterceptTouchEvent(true))。 如果用户抬起第一个手指(pointerId= 0),并且第二指(pointerId= 1)执行onInterceptTouchEvent ,

DrawerLayout不会存储该指针信息(pointerId=1) 当DrawerLayout得到ACTION_MOVE事件,它希望得到的pointerId指针信息。 然而,DrawerLayout只有指针​​

=0,mInitialMotionX的数组大小仍为1。所以导致ArrayIndexOutOfBoundsException异常。

解决方法:

自定义DrawerLayout 继承v4包的DrawerLayout

import android.content.Context;
import android.support.v4.widget.DrawerLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * Created by hbh on 2017/7/6.
 */

public class CustomDrawerLayout extends DrawerLayout {

    public CustomDrawerLayout(Context context) {
        super(context);
    }

    public CustomDrawerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    private boolean mIsDisallowIntercept = false;

    @Override
    public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
        // keep the info about if the innerViews do requestDisallowInterceptTouchEvent
        mIsDisallowIntercept = disallowIntercept;
        super.requestDisallowInterceptTouchEvent(disallowIntercept);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        // 错误的数组大小只会发生在多点触摸场景中。
        if (ev.getPointerCount() > 1 && mIsDisallowIntercept) {
            requestDisallowInterceptTouchEvent(false);
            boolean handled = super.dispatchTouchEvent(ev);
            requestDisallowInterceptTouchEvent(true);
            return handled;
        } else {
            return super.dispatchTouchEvent(ev);
        }
    }
}

将布局文件中使用的v4包的DrawerLayout替换为自定义的CustomDrawerLayout。

接下来你就可以对着你的手机一顿猛操,问题解决。

额。。。。。不好意思,加个注释哈。猛操:指反复进行多点触摸操作。

希望能帮到大家。


你可能感兴趣的:(Android)