Viewpager - 尝试catch处理,但指针索引超出范围

时间:2018-01-29 07:21:42

标签: android android-viewpager

我收到以下异常

 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)

我在应用程序中有2个viewpage.Both是自定义的 - 一个在onInterceptTouchEvent中返回false。另一个在onInterceptTouchEvent中实现了try catch。那么这个错误是怎么发生的? 为以下2个观看者添加了代码

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

根据要求为viewpagers添加了代码

3 个答案:

答案 0 :(得分:4)

我有同样的问题(感谢azi)

使用ViewPagerFixed修复你的错误

populateDate

您的xml文件

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

BottomBarViewPager可以扩展到ViewPagerFixed

答案 1 :(得分:1)

我使用的上述代码对我有用。 此错误Exception java.lang.IllegalArgumentException: pointerIndex out of range android.view.MotionEvent.nativeGetAxisValue (MotionEvent.java) android.view.MotionEvent.getX (MotionEvent.java:2141)

可能因为您可能正在使用的某种代码而发生。

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

答案 2 :(得分:0)

我遇到了同样的问题,并创建了一个自定义ViewPager来捕获异常。这是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
        }

}