pointerIndex out of range Android multitouch

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

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.


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

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!