Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Viewpager - Try catch handled but getting pointer index out of range

I am getting the following exception

 Exception java.lang.IllegalArgumentException: pointerIndex out of range
    android.view.MotionEvent.nativeGetAxisValue (MotionEvent.java)
    android.view.MotionEvent.getX (MotionEvent.java:2141)
    android.support.v4.view.ViewPager.onInterceptTouchEvent 
    (ViewPager.java:2092)
    android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2254)
    android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2711)
    android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2400)
    android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2711)
    android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2400)
    android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2711)
    android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2400)
    com.android.internal.policy.DecorView.superDispatchTouchEvent 
    (DecorView.java:416)
    com.android.internal.policy.PhoneWindow.superDispatchTouchEvent 
    (PhoneWindow.java:1837)
    android.app.Activity.dispatchTouchEvent (Activity.java:3154)
    com.android.internal.policy.DecorView.dispatchTouchEvent 
    (DecorView.java:378)
    android.view.View.dispatchPointerEvent (View.java:10177)
    android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent 
    (ViewRootImpl.java:4634)
    android.view.ViewRootImpl$ViewPostImeInputStage.onProcess 
    (ViewRootImpl.java:4502)
    android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3953)
    android.view.ViewRootImpl$InputStage.onDeliverToNext 
    (ViewRootImpl.java:4006)
    android.view.ViewRootImpl$InputStage.forward (ViewRootImpl.java:3972)
    android.view.ViewRootImpl$InputStage.apply (ViewRootImpl.java:3980)
    android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3953)
    android.view.ViewRootImpl$InputStage.onDeliverToNext 
    (ViewRootImpl.java:4006)
    android.view.ViewRootImpl$InputStage.forward (ViewRootImpl.java:3972)
    android.view.ViewRootImpl$AsyncInputStage.forward (ViewRootImpl.java:4101)
    android.view.ViewRootImpl$InputStage.apply (ViewRootImpl.java:3980)
    android.view.ViewRootImpl$AsyncInputStage.apply (ViewRootImpl.java:4158)
    android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3953)
    android.view.ViewRootImpl$InputStage.onDeliverToNext 
    (ViewRootImpl.java:4006)
    android.view.ViewRootImpl$InputStage.forward (ViewRootImpl.java:3972)
    android.view.ViewRootImpl$InputStage.apply (ViewRootImpl.java:3980)
    android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3953)
    android.view.ViewRootImpl.deliverInputEvent (ViewRootImpl.java:6443)
    android.view.ViewRootImpl.doProcessInputEvents (ViewRootImpl.java:6417)
    android.view.ViewRootImpl.enqueueInputEvent (ViewRootImpl.java:6378)
    android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent 
    (ViewRootImpl.java:6577)
    android.view.InputEventReceiver.dispatchInputEvent 
    (InputEventReceiver.java:185)
    android.view.InputEventReceiver.nativeConsumeBatchedInputEvents 
    (InputEventReceiver.java)
    android.view.InputEventReceiver.consumeBatchedInputEvents 
    (InputEventReceiver.java:176)
    android.view.ViewRootImpl.doConsumeBatchedInput (ViewRootImpl.java:6525)
    android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run 
    (ViewRootImpl.java:6600)
    android.view.Choreographer$CallbackRecord.run (Choreographer.java:871)
    android.view.Choreographer.doCallbacks (Choreographer.java:683)
    android.view.Choreographer.doFrame (Choreographer.java:613)
    android.view.Choreographer$FrameDisplayEventReceiver.run 
    (Choreographer.java:857)
    android.os.Handler.handleCallback (Handler.java:751)
    android.os.Handler.dispatchMessage (Handler.java:95)
    android.os.Looper.loop (Looper.java:154)
    android.app.ActivityThread.main (ActivityThread.java:6310)
    java.lang.reflect.Method.invoke (Method.java)
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run 
    (ZygoteInit.java:872)
    com.android.internal.os.ZygoteInit.main (ZygoteInit.java:762)

I have 2 viewpagers in the application.Both are custom - One returns false in onInterceptTouchEvent. The other has a try catch implemented in onInterceptTouchEvent. How is this error occuring then? Added code for the 2 viewpagers below

public class BottomBarViewPager extends ViewPager {

    public BottomBarViewPager(Context context) {
        super(context);
    }

    public BottomBarViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return false;
    }
}


public class GalleryViewPager extends ViewPager {

    public static final String TAG = GalleryViewPager.class.getSimpleName();

    private boolean swipeEnabled;

    public GalleryViewPager(Context context, AttributeSet attributes) {
        super(context, attributes);
        this.swipeEnabled = true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent te) {
        if (this.swipeEnabled) {
            return super.onTouchEvent(te);
        }

        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent me) {
        if (this.swipeEnabled) {


            /*
            This is caused by a bug in ViewPager and a solution is to catch the Exception
            and continue on.  This only happens if you quickly tap with 2 fingers in different
            locations, rapidly.


             */
            try {
                return super.onInterceptTouchEvent(me);
            } catch (IllegalArgumentException ex) {
//                SinclairLogger.e(TAG, "Caught exception in onInterceptTouchEvent", ex);
            }
        }

        return false;
    }

    public void setPagingEnabled(boolean enabled) {
        this.swipeEnabled = enabled;
    }
}

Added code for the viewpagers as requested

like image 940
Vibhuti Avatar asked Jan 29 '18 07:01

Vibhuti


2 Answers

i have same problem (thanks to azi)

use ViewPagerFixed fixed your bug

public class ViewPagerFixed extends android.support.v4.view.ViewPager {

public ViewPagerFixed(Context context) {
    super(context);
}

public ViewPagerFixed(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@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 xml file

<com.radesh.android.view.ViewPagerFixed xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

BottomBarViewPager can extend to ViewPagerFixed

like image 53
Radesh Avatar answered Nov 19 '22 08:11

Radesh


The above code I have used and it works for me. This error Exception java.lang.IllegalArgumentException: pointerIndex out of range android.view.MotionEvent.nativeGetAxisValue (MotionEvent.java) android.view.MotionEvent.getX (MotionEvent.java:2141)

might occur because of some sort of code like this which you might be using.

`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);
like image 26
niketshah09 Avatar answered Nov 19 '22 08:11

niketshah09