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 メソッドのオーバーライドを試して、スーパー.onInterceptTouchEvent(ev)をtry and catchで囲み、ViewPagerで私のために働いたことができます

@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 に答える