开发者

pointerIndex out of range Android multitouch

开发者 https://www.devze.com 2023-03-24 13:58 出处:网络
I have a touch event exception that is causing my game to crash on tablets (or more specifically, honeycomb)... My game works fine on my phone and I haven\'t heard of this happening to anyone that isn

I have a touch event exception that is causing my game to crash on tablets (or more specifically, honeycomb)... My game works fine on my phone and I haven't heard of this happening to anyone that isn't running Android 3.0 or higher. Here is the relevant log info...

E/AndroidRuntime(26487): java.lang.IllegalArgumentException: pointerIndex out of range
E/AndroidRuntime(26487):    at android.view.MotionEvent.nativeGetAxisValue(Native Method)
E/AndroidRuntime(26487):    at android.view.MotionEvent.getX(MotionEvent.java:1549)
E/AndroidRuntime(26487):    at kieran.android.asteroids.GameUI.onTouchEvent(GameUI.java:665)
E/AndroidRuntime(26487):    at android.view.View.dispatchTouchEvent(View.java:4616)
E/AndroidRuntime(26487):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1560)
E/AndroidRuntime(26487):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1291)
E/AndroidRuntime(26487):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1560)
E/AndroidRuntime(26487):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1291)
E/AndroidRuntime(26487):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1560)
E/AndroidRuntime(26487):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1291)

... And here is the code that is calling it. Seems fine to me, but maybe there is a bug in honeycomb that hasn't been fixed yet? The line number 665 from the log above corresponds to the float x = event.getX(id); line. It must have something to do with the way I am getting the which variable maybe? But it works fine on the phones/2.3 and lower...

int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;

if(actionCode == MotionEvent.ACTION_POINTER_UP || action == MotionEvent.ACTION_UP) {
    int which = action >> MotionEvent.ACTION_POINTER_ID_SHIFT;
    int id = event.getPointerId(which);
    float x = event.getX(id);
    float y = event.getY(id);

Any help/ideas would be greatly 开发者_如何学Goappreciated as I am trying to make my game available to tablet users as well. Thanks.


My problem was that it was actually calling event.getX(1) when there wasn't actually two ids. So I made sure that there were two ids with event.getPointerCount() >= 2 and it now works. Maybe you'll have the same luck!


For those who are still looking for answers please note that this may happen also when you are using any ViewPager with some Zooming or Scaling features on image etc inside it. Then there is a possibility you can get this Exception as well.

Solution:

Extend the existing ViewPager and override these methods with try catch.

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

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


Your missing a few things, you need to apply the mask to the pointer otherwise as you are not technically accessing the ID of the finger you think you are

int action = event.getAction() & MotionEvent.ACTION_MASK;     
int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
int pointerId = event.getPointerId(pointerIndex);


I'm also having this error but none of the solutions before the date of my post work. The only temporary pseudo-fix which works for me is to use a try-catch against IllegalArgumentException.

According to Android Issue 18990, it is related to ScaleGestureDetector and a fix has been committed few days ago.


The original posting is using the pointer id when the getX and getY use the pointer index.

It appears to work when you use the ID for a single touch because the id and the index are both 0. It will mess up if you use a multi-touch because the indices can change.

Example:

  • Touch 1 Down.
    Touch 1 State Index=0. ID=0

  • Touch 2 Down.
    Touch 1 State Index=0. ID=0
    Touch 2 State Index=1. ID=1

  • Touch 1 Release.
    Touch 2 State Index=0. ID=1

Try the following code:

int action = event.getAction();     
int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) 
                   >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;       
float x = event.getX(pointerIndex);
float y = event.getY(pointerIndex);


Just change your getPointerId() to findPointerIndex(). Maybe change geAction() to getActionMasked().

Hope it helps!


This worked for me :

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    try {
        return super.dispatchTouchEvent(ev);
    } catch (IllegalArgumentException ex) {
        ex.printStackTrace();
    }
    return false;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消