我想检测一扔事件一个简单的Android应用程序,但第一个 MotionEvent
参数总是空。为什么被称为一个空参数onFling方法?该Android文档说,当一个一扔事件发生下来MotionEvent初始和匹配起来它被称为MotionEvent。
类MyGestureDetector扩展SimpleOnGestureListener {
@覆盖
公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,浮velocityY){
// E1总是== null,则返回初空引用异常
如果(E1 == NULL){
返回false;
}
如果(e1.getY() - e2.getY()&≥120){
handleFlingEvent();
返回true;
}
返回false;
}
}
主要活动有这样的onTouchEvent方法:
GestureDetector flingDetector =新GestureDetector(新MyGestureDetector());
@覆盖
公共布尔的onTouchEvent(MotionEvent事件){
如果(flingDetector.onTouchEvent(事件)){
返回true;
}
返回super.onTouchEvent(事件);
}
解决方案
我已经得到了类似的问题。它发生在当GestureDetector和onFling方法是在自定义视图类,这是该活动的用户界面的一部分。经过一系列的测试中我发现,只有主活动可以检测到扫动手势良好。所以,我解决他们替换活动的问题。但我没有找到这个问题的根本原因。
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);
}
解决方案
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.