34

我的自定义视图中有这个方法,它扩展了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);
}

偶尔 - 我收到以下异常:

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)

我也尝试过使用onInterceptTouchEvent()并得到了同样的例外。

这是一个额外的堆栈跟踪:

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

3 回答 3

37

2.1 (Eclair) 中存在一个错误,其中未检查指针索引,有时为-1。您可以创建一个扩展 ViewPager 的自定义视图,覆盖 onTouchEvent 并将对 super.onTouchEvent 的调用包装在 try/catch 中。虽然不是一个非常漂亮的修复,但我记得它没有副作用(比如错过触摸事件)。

于 2013-05-13T10:30:15.390 回答
15

添加到上面的答案您也可以尝试覆盖 onInterceptTouchEvent 方法并使用 try and catch 在 ViewPager 中为我环绕 super.onInterceptTouchEvent(ev)

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    try {
        return super.onInterceptTouchEvent(ev);
    } catch (IllegalArgumentException ex) {
        ex.printStackTrace();
    }
    return false;
}
于 2015-07-09T01:45:46.173 回答
2

我遇到了同样的问题,我创建了一个自定义 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
        }
}
于 2019-04-08T10:45:44.957 回答