我在onTouch中使用GestureDetector进行了一项活动。在我的布局中,我还有一个实现onClickListener的视图。在Android ICS上,我在处理TouchEvent时遇到NullPointerException。这个错误的原因是什么?我看过几篇帖子说onClick和onTouch不能很好地结合在一起,但是当看到这个错误时,没有真正的原因解释或问题的真正解决方案。
以下是代码:
public class FlipCardActivity extends Activity implements
View.OnClickListener, View.OnTouchListener {
protected GestureDetector gestureDetector;
protected class TouchSwipeListener extends GestureDetector.
SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// Calculate dx, gradient, velocity, etc
// Check for and discard unacceptable swipes
if (Math.abs(gradient) > SWIPE_MAX_GRADIENT || Math.abs(distance) <
SWIPE_MIN_DISTANCE || Math.abs(velocity) < SWIPE_MIN_VELOCITY)
return false;
// Determine whether it's a left or a right swipe
if (dx < 0)
activity.showNext();
else
activity.showPrevious();
return true;
}
}
@Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
gestureDetector = new GestureDetector(this, new TouchSwipeListener(this));
setContentView(R.layout.main);
}
@Override
public boolean onTouch(View v, MotionEvent ev) {
return gestureDetector.onTouchEvent(ev);
}
}
堆栈跟踪是:
java.lang.NullPointerException
at android.view.GestureDetector.onTouchEvent(GestureDetector.java:587)
at com.fivepumpkins.common.FlipCardActivity.onTouch(FlipCardActivity.java:602)
at android.view.View.dispatchTouchEvent(View.java:5536)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1912)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1371)
at android.app.Activity.dispatchTouchEvent(Activity.java:2364)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1860)
at android.view.View.dispatchPointerEvent(View.java:5721)
at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2890)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2466)
at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:845)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2475)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4575)
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:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
查看ICS Source,我可以提取错误:GestureDetector.java:587
。它发生在onTouchEvent()
方法内,case MotionEvent.ACTION_UP:
。以下是摘录:
// Hold the event we obtained above - listeners may have changed the original.
586 mPreviousUpEvent = currentUpEvent;
587 mVelocityTracker.recycle();
588 mVelocityTracker = null;
589 mIsDoubleTapping = false;
590 mHandler.removeMessages(SHOW_PRESS);
591 mHandler.removeMessages(LONG_PRESS);
592 break;
第587行中的NullPointerException
表示mVelocityTracker
为空。这是VelocityTracker
类的私有SimpleOnGestureListener
属性。为什么此变量在此时为空?
答案 0 :(得分:1)
它确实与点击监听器有关。 GestureDetector访问mVelocityTracker并在MotionEvent.ACTION_UP和MotionEvent.ACTION_CANCEL中将其设置为null。 Click侦听器在View类的onTouchEvent方法的MotionEvent.ACTION_UP中执行。可能存在一个关系(我现在没时间找到它)与GestureDetector执行的触发器,在点击后,MotionEvent.ACTION_UP或MotionEvent.ACTION_CANCEL,之后已被其中一个设置为null。
尝试删除点击监听器并查看是否仍然发生。如果是,您可能更好地处理触摸侦听器中的所有内容。
答案 1 :(得分:1)
我在onTouchEvent()
获得了NPE:587。所以我开始使用支持库中的GestureDetectorCompat
,它工作正常。