我在 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 中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 行中的 ANullPointerException
表示mVelocityTracker
为空。这是类的私有VelocityTracker
属性SimpleOnGestureListener
。为什么此时此变量为空?