2012-11-28 24 views
1

起初,我真的知道有不止一個帖子,但沒有幫助我修復錯誤/ misstake。我很抱歉,如果它的轉發,並希望你們可以幫助我擺脫這個錯誤。pointerIndex超出範圍multitouch

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

在我的那個開關調用methodes我只是獲取觸摸的位置並處理它們。我只是可以把一個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; 
    } 
} 
+0

你可以將它與任何拋出錯誤的大小()進行比較,並確保它不太大。 – codeMagic

回答

4

儘量確保pointerIndex < event.getPointerCount();

但我認爲,也許你有一個誤解這裏。代碼中的pointerIndex是已更改指針的索引,它只應在ACTION_POINTER_DOWN和ACTION_POINTER_UP中使用。如果屏幕上只有一個手指,則pointerIndex在此處無效。

+0

好吧,它實際上使用2個手指,但有時它與該代碼崩潰。但是隻有當我觸摸得非常快時它纔會這樣做。我爲你說的話添加了一個if。 – BennX

+0

請發佈processJoyPadTouchRelease()的代碼。 – faylon

+0

我添加了代碼。它應該正常工作,因爲您也可以通過其ID來獲得第一個觸摸事件。嘗試與logcat – BennX