java.lang.ArrayIndexOutOfBoundsException: length=1; index=1

一个页面同时存在PhotoView与DrawerLayout,DrawerLayout组件抛出一个ArrayIndexOutOfBoundsException异常的shouldInterceptTouchEvent。请参阅下面的堆栈跟踪:
 
03-19 15:32:36.720: E/AndroidRuntime(5284): FATAL EXCEPTION: main
03-19 15:32:36.720: E/AndroidRuntime(5284): java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.support.v4.widget.ViewDragHelper.shouldInterceptTouchEvent(ViewDragHelper.java:1004)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.support.v4.widget.DrawerLayout.onInterceptTouchEvent(DrawerLayout.java:820)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1817)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.view.View.dispatchPointerEvent(View.java:7421)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:171)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4342)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4382)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.view.Choreographer.doCallbacks(Choreographer.java:562)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.view.Choreographer.doFrame(Choreographer.java:530)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.os.Handler.handleCallback(Handler.java:725)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.os.Handler.dispatchMessage(Handler.java:92)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.os.Looper.loop(Looper.java:137)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at android.app.ActivityThread.main(ActivityThread.java:5039)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at java.lang.reflect.Method.invokeNative(Native Method)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at java.lang.reflect.Method.invoke(Method.java:511)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-19 15:32:36.720: E/AndroidRuntime(5284): 	at dalvik.system.NativeStart.main(Native Method)
 
 
这是关于多点触摸的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异常

解决办法:

public class HackyDrawerLayout extends DrawerLayout {

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

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

    public HackyDrawerLayout(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) {
        // the incorrect array size will only happen in the multi-touch scenario.
        if (ev.getPointerCount() > 1 && mIsDisallowIntercept) {
            requestDisallowInterceptTouchEvent(false);
            boolean handled = super.dispatchTouchEvent(ev);
            requestDisallowInterceptTouchEvent(true);
            return handled;
        } else {
            return super.dispatchTouchEvent(ev);
        }
    }
}





评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值