Android中的ViewDragHelper中的ArrayIndexOutOfBoundsException

时间:2014-07-30 17:28:17

标签: android debugging exception android-studio

我有一个问题是找出我的源代码中的错误。

有什么方法可以解决它?

我的代码中抛出了“ArrayIndexOutOfBoundsException”,但我找不到它。

android studio中的

logcat

07-29 22:31:26.745  11782-11782/com.example.petr.myapp1 E/InputEventReceiver﹕ Exception dispatching input event.
07-29 22:31:26.745  11782-11782/com.example.petr.myapp1 D/AndroidRuntime﹕ Shutting down VM
07-29 22:31:26.745  11782-11782/com.example.petr.myapp1 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41595ba8)
07-29 22:31:26.745  11782-11782/com.example.petr.myapp1 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.petr.myapp1, PID: 11782
    java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
            at android.support.v4.widget.ViewDragHelper.shouldInterceptTouchEvent(ViewDragHelper.java:1004)
            at android.support.v4.widget.DrawerLayout.onInterceptTouchEvent(DrawerLayout.java:979)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1859)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
            at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
            at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
            at android.view.View.dispatchPointerEvent(View.java:7886)
            at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3947)
            at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3826)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3442)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3411)
            at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3518)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3419)
            at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3575)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3442)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3411)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3419)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
            at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5532)
            at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5512)
            at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5483)
            at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5612)
            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:5585)
            at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:5631)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
            at android.view.Choreographer.doFrame(Choreographer.java:542)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
            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:5001)
            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:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:2)

由于this.,我能够解决这个问题。要解决此问题,需要在ViewGroup中重写两个方法。

  • requestDisallowInterceptTouchEvent(boolean disallowIntercept)
  • dispatchTouchEvent(MotionEvent ev)

requestDisallowInterceptTouchEvent设置如下:

@Override
public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
    Log.i(TAG, "In requestDisallowInterceptTouchEvent!!!");
    mIsDisallowIntercept = disallowIntercept;
    super.requestDisallowInterceptTouchEvent(disallowIntercept);
}

存储disallowIntercept localy:

private boolean mIsDisallowIntercept = false;

然后将dispatchTouchEvent设置为:

@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);
    }
}

其他解决方案包括覆盖onInterceptTouchEvent,如下所示:

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) { 
        if( _locked ) {
            return false;
        } else {
            try{
                return super.onInterceptTouchEvent(ev);
            }catch(IllegalArgumentException ex){
                ex.printStackTrace();
            }
        }
        return false;
}

这种方法对我没有帮助,但它可能对其他人有帮助。找到此解决方案here.

我希望这会有所帮助。

答案 1 :(得分:0)

谷歌在https://code.google.com/p/android/issues/detail?id=182262

报道了类似的问题

但在Android支持库发布后,修订版23.2.1(2016年3月)此问题已得到解决。

将支持库更新为Android Support Library to 23.2.1

答案 2 :(得分:0)

在Google ViewDragHelper.java saveLasMotion java.lang.ArrayIndexOutOfBoundsException

上查看此问题

似乎在Android支持库24.1.0中解决了。所以更新到那个或设置try catch到变通方法