java.lang.IllegalArgumentException: pointerIndex out of range Exception - dispatchTouchEvent

Daniel L. picture Daniel L. · May 9, 2013 · Viewed 19.8k times · Source

I have this method in my custom view which extends FrameLayout:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    int currentItem = vp.getCurrentItem();
    if (inDragPanelZone(currentItem, ev.getX(), ev.getY()) && ev.getAction() == MotionEvent.ACTION_DOWN) {      
        if (!shouldAllowPaging()) { 
            return true; 
        }
        vp.setPagingEnabled(true);
    }
    return super.dispatchTouchEvent(ev);
}

Once in a while - I'm getting the following Exception:

java.lang.IllegalArgumentException: pointerIndex out of range
at android.view.MotionEvent.nativeGetAxisValue(Native Method)
at android.view.MotionEvent.getX(MotionEvent.java:1995)
at android.support.v4.view.MotionEventCompatEclair.getX(MotionEventCompatEclair.java:32)
at android.support.v4.view.MotionEventCompat$EclairMotionEventVersionImpl.getX(MotionEventCompat.java:86)
at android.support.v4.view.MotionEventCompat.getX(MotionEventCompat.java:210)
at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:1734)
at android.view.View.dispatchTouchEvent(View.java:5710)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1951)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1712)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
at MyView.dispatchTouchEvent(DraggablePane.java:176)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1918)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1377)
at android.app.Activity.dispatchTouchEvent(Activity.java:2391)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1866)
at android.view.View.dispatchPointerEvent(View.java:5890)
at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3077)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2619)
at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:983)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4512)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
at dalvik.system.NativeStart.main(Native Method)

I also tried using onInterceptTouchEvent() and got the same exception.

Here is an additional stacktrace:

java.lang.IllegalArgumentException: pointerIndex out of range
    at android.view.MotionEvent.nativeGetAxisValue(Native Method)
    at android.view.MotionEvent.getX(MotionEvent.java:2013)
    at android.support.v4.view.MotionEventCompatEclair.getX(MotionEventCompatEclair.java:32)
    at android.support.v4.view.MotionEventCompat$EclairMotionEventVersionImpl.getX(MotionEventCompat.java:86)
    at android.support.v4.view.MotionEventCompat.getX(MotionEventCompat.java:210)
    at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:1734)
    at android.view.View.dispatchTouchEvent(View.java:5724)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1964)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1725)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1970)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1739)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1970)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1739)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1970)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1739)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1970)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1739)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2071)
    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
    at android.app.Activity.dispatchTouchEvent(Activity.java:2426)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2019)
    at android.view.View.dispatchPointerEvent(View.java:5904)
    at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3155)
    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2670)
    at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:1000)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1014)
    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2644)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4517)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
    at dalvik.system.NativeStart.main(Native Method)

Answer

FunkTheMonk picture FunkTheMonk · May 13, 2013

There is a bug in 2.1 (Eclair) where the pointer index isn't checked and is sometimes -1. You can create a custom view that extends ViewPager, override onTouchEvent and wrap the call to super.onTouchEvent in a try/catch. Whilst not a very pretty fix, I remember that it has no side effects (like missed touch events).