Alex Rivera | Logout

Android SimpleOnGestureListener.onFling getting a null MotionEvent

Asked 2010-11-11T04:35:15.313
23

I am trying to detect a fling event on a simple Android application, but the first MotionEvent argument is always null. Why is the onFling method being called with a null argument? The Android documentation says that it gets called when a fling event occurs with the initial on down MotionEvent and the matching up MotionEvent.

class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        // e1 always == null, return early a null reference exception
        if (e1 == null) {
            return false;
        }
        if (e1.getY() - e2.getY() > 120) {
            handleFlingEvent();
            return true;
        }
        return false;
    }
}

The main Activity has this onTouchEvent method:

GestureDetector flingDetector = new GestureDetector(new MyGestureDetector());

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (flingDetector.onTouchEvent(event)) {
        return true;
    }
    return super.onTouchEvent(event);
}
Edit
Report

1 Answer

5

I've got the same problem with custom view and OnGestureListener onFling. Found out this:

Whether or not you use GestureDetector.SimpleOnGestureListener, you must always implement an onDown() method that returns true.

Sourced from http://developer.android.com/training/custom-views/making-interactive.html

answered 2012-11-12T10:14:24.860

Your Answer