36

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)
Daniel L.
  • 5,060
  • 10
  • 36
  • 59

3 Answers3

42

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

FunkTheMonk
  • 10,908
  • 1
  • 31
  • 37
  • I saw this bug, and I've used the workaround you suggested before, but I'm trying to find a way to handle it properly.. – Daniel L. May 13 '13 at 10:49
  • 1
    There was an update to the support library recently which may have fixed this error (as in, they fixed ViewPager, not the causing bug in 2.1). Haven't checked it out. – FunkTheMonk Oct 14 '13 at 14:11
  • 3
    @FunkTheMonk nope the bug is still there :( – martyglaubitz Sep 11 '14 at 11:25
  • I've still got this bug, but the suggested fix works – user1743524 Oct 28 '14 at 21:12
  • Did anyone see the same behaviour in android 4.4.4? I see a stacktrace where `at android.support.v4.view.MotionEventCompatEclair.c(MotionEventCompatEclair.java:32)` is in front of `at android.view.MotionEvent.getX(MotionEvent.java:2227)` and it results in the above mentioned exception... – prom85 Jun 16 '15 at 07:15
  • @prom85 I answered your question here: http://stackoverflow.com/questions/28670970/viewpager-java-lang-illegalargumentexception-pointerindex-out-of-range/31459946#31459946 – jonathanrz Jul 16 '15 at 16:42
15

Adding to the above answer Also you can try Overriding onInterceptTouchEvent method and surround super.onInterceptTouchEvent(ev) with try and catch worked for me in a ViewPager

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    try {
        return super.onInterceptTouchEvent(ev);
    } catch (IllegalArgumentException ex) {
        ex.printStackTrace();
    }
    return false;
}
Mohamed
  • 761
  • 9
  • 19
3

I had the same problem and I created a custom ViewPager to catch the exception. Here is the solution in kotlin:

class CustomViewPager : ViewPager {

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean =
        try {
            super.onInterceptTouchEvent(ev)
        } catch (e: IllegalArgumentException) {
            //uncomment if you really want to see these errors
            //e.printStackTrace();
            false
        }
}
Noelia
  • 3,698
  • 4
  • 17
  • 22