问题描述
我用onTouch一个GestureDetector活动。在我的布局,我也有一个观点实现的onClickListener。在Android ICS,我得到一个NullPointerException异常,而处理的TouchEvent。什么是此错误的原因是什么?我见过几个职位说的onClick和onTouch不玩好起来,但原因还是真正的解决问题的方法没有真正的解释,当人们看到这个错误。
I have an activity using a GestureDetector in onTouch. Inside my layout, I also have a view implementing an onClickListener. On Android ICS, I get a NullPointerException while handling the TouchEvent. What is the cause of this error? I have seen several posts saying that onClick and onTouch don't play well together but no real explanation of the cause or a real solution to the problem, when one sees this error.
下面是code:
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源,我可以提取使用错误的行: GestureDetector.java:587
。它发生在的onTouchEvent()
方法中,在案件案例MotionEvent.ACTION_UP:
。下面是摘录:
Looking at the ICS Source, I can extract the line with the error: GestureDetector.java:587
. It happens inside the onTouchEvent()
method, in the case case MotionEvent.ACTION_UP:
. Here is the excerpt:
// 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;
A NullPointerException异常
在587线意味着 mVelocityTracker
为空。这是民营 VelocityTracker
的 SimpleOnGestureListener
类的属性。为什么会这样变量为空在这一点?
A NullPointerException
in Line 587 implies that mVelocityTracker
is null. This is the private VelocityTracker
attribute of the SimpleOnGestureListener
class. Why would this variable be null at this point?
推荐答案
这可能与点击听众所确实有关。该GestureDetector,访问mVelocityTracker并将其设置在MotionEvent.ACTION_UP和MotionEvent.ACTION_CANCEL为null。点击监听器是在MotionEvent.ACTION_UP的onTouchEvent的View类的方法执行。有可能的关系(我没有时间,现在找)与触发器的GestureDetector执行,对点击,MotionEvent.ACTION_UP或MotionEvent.ACTION_CANCEL后,它已经被设置为空,由其中之一,之前。
It could be indeed related with the click listener. The GestureDetector, accesses mVelocityTracker and sets it to null in MotionEvent.ACTION_UP and MotionEvent.ACTION_CANCEL. The click listener is executed in the MotionEvent.ACTION_UP of onTouchEvent method of View class. There's maybe a relation (which I don't have time to find now) with triggers that the GestureDetector executes, on click, MotionEvent.ACTION_UP or MotionEvent.ACTION_CANCEL, after it has been already set to null, by one of these, before.
尝试取出点击监听器,看看它是否仍然发生。如果是的话,你可能更好地处理一切都在触摸监听器。
Try removing the click listener and see if it still happens. If yes, you are probably better handling everything in the touch listener.
这篇关于NullPointerException异常的GestureDetector.onTouchEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!