10

我在 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。为什么此时此变量为空?

4

2 回答 2

2

我还在onTouchEvent():587 中获得了 NPE。所以我GestureDetectorCompat从支持库开始使用它,它工作正常。

于 2014-02-28T13:37:06.280 回答
1

它确实可能与点击监听器有关。GestureDetector 访问 mVelocityTracker 并在 MotionEvent.ACTION_UP 和 MotionEvent.ACTION_CANCEL 中将其设置为 null。点击监听在 View 类的 onTouchEvent 方法的 MotionEvent.ACTION_UP 中执行。可能与 GestureDetector 在单击时执行的触发器、MotionEvent.ACTION_UP 或 MotionEvent.ACTION_CANCEL 之间存在关系(我现在没有时间找到),之前它已经被其中之一设置为 null。

尝试删除点击监听器,看看它是否仍然发生。如果是,您可能会更好地处理触摸侦听器中的所有内容。

于 2012-07-08T12:57:28.467 回答