0

我已经在 stackoverflow 上搜索了解决方案,但没有与我匹配的方案。我有一个由滚动视图包裹的文本视图:

    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/titlefav" >

        <TextView
            android:id="@+id/contentText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </TextView>
    </ScrollView>

我有一个用于 onfling 的手势检测器,并将此侦听器绑定到 textview 和 scrollview:

class MyGestureDetector extends SimpleOnGestureListener {

    final String TAG = MyGestureDetector.class.getSimpleName();

    // for touch left or touch right events
    private static final int SWIPE_MIN_DISTANCE = 40; // default is 120
    private static final int SWIPE_MAX_OFF_PATH = 600;
    private static final int SWIPE_THRESHOLD_VELOCITY = 50;

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2,
            float velocityX, float velocityY) {

        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                onHorizonTouch(true); // left
            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                onHorizonTouch(false); // right
            }
        } catch (Exception e) {
            // nothing
        }
        return false;
    }
}
gestureDetector = new GestureDetector(new MyGestureDetector());
View.OnTouchListener gestureListener = new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (gestureDetector.onTouchEvent(event)) {
            return true;
        }
        return false;
    }
};
tv.setOnTouchListener(gestureListener);
sv.setOnTouchListener(gestureListener);

现在,当用户尝试触摸或滑动时(我不确定),我在 Google Play 中看到以下异常,但没有提及 MainActivity。知道如何解决这个问题。

java.lang.IllegalArgumentException: pointerIndex out of range
at android.view.MotionEvent.nativeGetAxisValue(Native Method)
at android.view.MotionEvent.getY(MotionEvent.java:2090)
at android.widget.ScrollView.onTouchEvent(ScrollView.java:839)
at android.view.View.dispatchTouchEvent(View.java:7337)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2410)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2145)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2115)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1468)
at android.app.Activity.dispatchTouchEvent(Activity.java:2487)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2063)
at android.view.View.dispatchPointerEvent(View.java:7520)
at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3376)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3308)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4402)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4380)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4484)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:163)
at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4452)
at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4503)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:523)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
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:1006)
at com.android.internal.os.ZygoteInit.main(Zygo
4

3 回答 3

2

尝试以下操作:

gestureDetector = new GestureDetector(getActivity(), new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
};
getView().setOnTouchListener(gestureListener);
    ScrollView scrollView = (ScrollView) getView().findViewById(R.id.scroll_view);
    scrollView.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (gestureDetector != null) 
                return gestureDetector.onTouchEvent(event);
            return false;
        }
    });
于 2013-07-29T14:17:57.637 回答
0

我遇到了同样的问题并使用该问题的答案中的方法解决了它:

java.lang.IllegalArgumentException:pointerIndex 超出范围异常 - dispatchTouchEvent

似乎奏效了。

于 2014-05-21T15:34:14.607 回答
0

对我来说唯一的解决方案是注释掉 onDown(MotionEvent e) 方法

/*
@Override
    public boolean onDown(MotionEvent e) {
        return true;
}
*/
于 2017-05-19T14:43:50.107 回答