22

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);
}

6 Answers 6

15

I first ran into this issue with Android 4.0 as my code works fine in all the prior versions. After taking a cue from the suggestions here, I've found an easy workaround is to just keep track of the onDown event and use it in place of the first param of onFling in case it happens to be null.

public class MySimpleGestureListener extends SimpleOnGestureListener {
    protected MotionEvent mLastOnDownEvent = null;

    @Override
    public boolean onDown(MotionEvent e) {
        mLastOnDownEvent = e;
        return true;//super.onDown(e);
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        if (e1==null)
            e1 = mLastOnDownEvent;
        if (e1==null || e2==null)
            return false;
        float dX = e2.getX()-e1.getX();
        float dY = e2.getY()-e1.getY();
        ...rest of code
    }

    ...rest of class
}
2
  • 2
    I tried this, but it hasn't fixed the issue for me. onDown isn't called and e1 is still null. Mar 17, 2016 at 21:18
  • @fobbymaster in onDown callback just return true instead of super.onDown(e)
    – 0neel
    Dec 5, 2016 at 14:59
6

In custom view group class you need override 2 methods: onInterceptTouchEvent and onTouchEvent. After that you need to call GestureDetector.onTouchEvent() in both methods. Event ACTION_DOWN is passed to onInterceptTouchEvent only.

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    if(mDetector.onTouchEvent(event)) {
        return true;
    }
    return super.onInterceptTouchEvent(event);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if(mDetector.onTouchEvent(event)) {
        return true;
    }
    return super.onTouchEvent(event);
}
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

1
  • Agree. onDown() must return true, otherwise no other subsequent call will get detected. onScroll() / onFling won't simply get called.
    – Alex
    Dec 30, 2019 at 22:53
2

I have got the similar problem. It happens when the GestureDetector and the onFling method was in a custom view class, which is a part of the activity UI. After a series of testing I found that only the main Activity can detect the swipe gesture well. So I solve the problem by replace them in the activity. But I didn't find the root cause of this problem.

5
  • I got it to work, but I was never able to determine what finally did it. Your solution does apply in my case, so I accepted your answer. I would still like to know why it didn't work.
    – mcrumley
    Jan 5, 2011 at 7:14
  • Me too. I'd like to know why it didn't work as well. Hope somebody answer that.
    – Cable W
    Jan 8, 2011 at 9:01
  • Have you had any luck tracking the cause of this down? I'm suffering from the same problem but can't seem to get it to work even with the onFling method being in the main activity. Jan 30, 2011 at 11:39
  • 5
    Just figured out what was causing this for me. I added an OnLongPressListener to a child view inside the ScrollView from which I was trying to get the fling event. This caused the ACTION_DOWN event to get gobbled up and never make it to the GestureDetector which was why the initial motion event is never set. Solved it by handling the OnLongPress event in the OnGestureListener and manually determining which child has been pressed. Jan 30, 2011 at 12:18
  • 4
    This problem is caused by other listeners(whatever added by yourself or introduced by some views, for example, scrollview) who decide to "consume" the event instead of letting the event travel along the event processing chain. That is when Activity.dispatchTouchEvent(MotionEvent e) is needed, and here is a similar question on it: stackoverflow.com/questions/7137742/… I am still looking for a detailed document on how Android handle MotionEvent but so far with no luck on it.
    – Jerry Tian
    Jul 24, 2013 at 18:10
2

I had 3 buttons in horizonal linear layout and this layout in turn surrounded by horizontalscrollview.

Then I set simplegesturelistener on the horizontalscrollview and got the same error as above. Then i also set simplegesturelistener for all the buttons in the layout and it worked.

1

I think the onDown event has been resumed by the scrollview, so we can't get the event in the activity. If you custom a ScrollView and override the dispatchTouchEvent() and onTouchEvent(), the problem will be solved.

Activity:

public class TestActivity extends Activity {
     private MyScrollView scrollView;
     private TestActivity me;
     private GestureDetector detector; 

     protected void onCreate(Bundle savedInstanceState) {
            this.me = this;
            MySimpleGestureListener gestureListener = new MySimpleGestureListener();
            this.detector = new GestureDetector(gestureListener);
            this.scrollView.setOnTouchListener(onTouchListerner);
            this.scrollView.setDetector(detector);
     }

     private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return me.detector.onTouchEvent(event);
    }
 };
}

MySimpleGestureListener:

public class MySimpleGestureListener extends SimpleOnGestureListener {
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        return super.onScroll(e1, e2, distanceX, distanceY);
    }

    @Override
    public void onLongPress(MotionEvent e) {
    }


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

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
                    // TODO do your onFling here

    }
}

At last, MyScrollView

public class MyScrollView extends ScrollView {
    private GestureDetector detector;

    @Override
public boolean dispatchTouchEvent(MotionEvent ev){
    if (this.detector!= null) {
            detector.onTouchEvent(ev);
        super.dispatchTouchEvent(ev);
        return true;
    }
    return super.dispatchTouchEvent(ev);
} 

@Override
public boolean onTouchEvent(MotionEvent ev) {
    super.onTouchEvent(ev);
    //
    if (this.detector != null) {
        return this.detector.onTouchEvent(ev);
    } 
    return false;
}

    // here is the getter and setter
}

Hope this help.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.