【问题标题】:pointerIndex out of Range multitouch指针索引超出范围多点触控
【发布时间】:2012-11-28 16:49:02
【问题描述】:

起初,我真的知道有不止一篇关于它的帖子,但没有帮助我修复错误/错误。很抱歉,如果它是一个转发,希望你们能帮助我摆脱这个错误。

11-28 17:36:36.110: W/dalvikvm(8380): threadid=1: thread exiting with uncaught exception (group=0x40a331f8)
11-28 17:36:36.110: E/AndroidRuntime(8380): FATAL EXCEPTION: main
11-28 17:36:36.110: E/AndroidRuntime(8380): java.lang.IllegalArgumentException: pointerIndex out of range
11-28 17:36:36.110: E/AndroidRuntime(8380):     at android.view.MotionEvent.nativeGetAxisValue(Native Method)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at android.view.MotionEvent.getX(MotionEvent.java:1974)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at com.main.TouchEventControler.processJoyPadTouchRelease(TouchEventControler.java:194)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at com.main.TouchEventControler.processRelease(TouchEventControler.java:237)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at com.main.TouchEventControler.processTouchEvent(TouchEventControler.java:88)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at com.main.GameField.onTouchEvent(GameField.java:142)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at android.view.View.dispatchTouchEvent(View.java:5546)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1938)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1375)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at android.app.Activity.dispatchTouchEvent(Activity.java:2364)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1886)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at android.view.View.dispatchPointerEvent(View.java:5726)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2890)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2466)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:845)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2475)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at android.os.Looper.loop(Looper.java:137)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at android.app.ActivityThread.main(ActivityThread.java:4424)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at java.lang.reflect.Method.invokeNative(Native Method)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at java.lang.reflect.Method.invoke(Method.java:511)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-28 17:36:36.110: E/AndroidRuntime(8380):     at dalvik.system.NativeStart.main(Native Method)

我确实像这样处理它们,我很高兴它有时会像我想要的那样运行,如果它没有因该错误而崩溃:

public void processTouchEvent(MotionEvent event) {

    // create an action
    int action = (event.getActionMasked() & MotionEvent.ACTION_MASK);
    // getting pointerindex
    int pointerIndex = ((event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT);
    // getting pointer id

    //catch invalide
    if(pointerIndex == MotionEvent.INVALID_POINTER_ID){
        return;
    }

    int pointerId = event.findPointerIndex(pointerIndex);
    // switch case for the action
    switch (action) {
    case MotionEvent.ACTION_DOWN: // if action down
        processHitButtonTouch(event, pointerId);
        processJoyPadTouch(event, pointerId);
        break;
    case MotionEvent.ACTION_POINTER_DOWN: // if pointer down (second
                                            // finger/third)
        processHitButtonTouch(event, pointerId);
        processJoyPadTouch(event, pointerId);
        break;
    case MotionEvent.ACTION_UP: // if action up
        processRelease(event, pointerId);
    case MotionEvent.ACTION_POINTER_UP: // if action up second/third finger
        processRelease(event, pointerId);
    default:
        break;
    }
}

在我调用该开关的方法中,我只是获取触摸的位置并处理它们。我可以将 getX(pointerIndex) 放在 try catch 中,如果出错则返回。但是没有更好的方法吗?!

非常感谢我已经获得的所有帮助。

要求:

    public void processJoyPadTouchRelease(MotionEvent event, int pointerId) {
    int touchPosX = (int) event.getX(pointerId);
    int touchPosY = (int) event.getY(pointerId);

    if (( //dont freak out here! it's just looking for if the
            //touch was inside the Joypad! Nothing more!!
            //it's in breckets!
            touchPosX > 2 * Config.BLOCKSIZE
            && touchPosX < 4 * Config.BLOCKSIZE
            && touchPosY > 3 * Config.BLOCKSIZE
            && touchPosY < 5 * Config.BLOCKSIZE
            // touch up event handled
            || 2 * Config.BLOCKSIZE < touchPosX
            && touchPosX < (Config.BLOCKSIZE * 4)
            && touchPosY > (7 * Config.BLOCKSIZE)
            && touchPosY < (9 * Config.BLOCKSIZE)
            // touch down handled
            || (touchPosX < 2 * Config.BLOCKSIZE
            && touchPosY > (5 * Config.BLOCKSIZE) 
            && touchPosY < (7 * Config.BLOCKSIZE))
            // touch left handled
            || (4 * Config.BLOCKSIZE < touchPosX
            && touchPosX < (Config.BLOCKSIZE * 6)
            && touchPosY > (5 * Config.BLOCKSIZE) 
            && touchPosY < 7 * Config.BLOCKSIZE)) 
            //touch right handled

            //and the char isnt idle (so it's moving)
        && charac.getStatus() != Status.IDLE)
    //THAN do =>>
    {
        charac.setStatus(Status.IDLE);
        joyPad.status = Status.IDLE;
    }
}

【问题讨论】:

  • 您可以将它与引发错误的任何东西的 size() 进行比较,并确保它不会太大。

标签: android multi-touch outofrangeexception


【解决方案1】:

尽量确保pointerIndex &lt; event.getPointerCount();

但我认为你在这里可能有一个误解。代码中的 pointerIndex 是更改的指针的索引,它只能用于 ACTION_POINTER_DOWN 和 ACTION_POINTER_UP。如果你只有一根手指在屏幕上,这里的pointerIndex是无效的。

【讨论】:

  • 它实际上可以用 2 个手指工作,但有时它会因该代码而崩溃。但只有当我触摸得非常快时它才会这样做。我为你所说的情况添加了一个 if。
  • 我添加了代码。它应该正常工作,因为您也可以通过其 ID 获取第一个触摸事件。用 logcat 试了一下
  • 哦,我的天哪,很抱歉没有看到这个错误,非常感谢您的错误修复!
猜你喜欢
  • 1970-01-01
  • 2014-01-07
  • 2011-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-31
  • 2018-03-01
相关资源
最近更新 更多