Sandeep Sandeep -4 years ago 197
Android Question

pointerIndex out of range in Android ScrollView

I have searched for the solution on stackoverflow but there is no scenario matching mine. I have a textview wrapped by scrollview:

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


I have a gesture detector for onfling and have bound this listener to both textview as well 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);


Now when the user is either trying to touch or swipe (I am not sure), I see following exception in Google Play with no mention of MainActivity. Any idea how can I resolve this issue.

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

Answer Source

Try the following:

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;
        }
    });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download