Android TextView NullPointerException with onTouchListener and onClickListener on 4.0

I have a TextView that I have assigned both onTouchListener and onClickListener:

myTextView.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { _gestureDetector.onTouchEvent(event); return false; } }); myTextView.setOnClickListener(new OnClickListener() { public void onClick(View v) { // Show Toast Notification } }); 

OnTouchListener will detect onFling () for the gesture detector. This code works without any problems. EXCEPT for Android 4.0 (Ice Cream Sandwich). From 4.0 I get a NullPointerException in "_gestureDetector.onTouchEvent (event)"; (when I try to quit).

If I comment onClickListener, however, fling will work and I will not get a NullPointerException.

I was on the assumption that if both touch listeners return with an error, the event will not be consumed, which will allow them to work.

Does anyone have any ideas? Thanks!

Here is my code:

  /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.calendar); _gestureDetector = new GestureDetector(new GestureListener()); } private class GestureListener extends SimpleOnGestureListener { @Override public boolean onDown(MotionEvent e) { return false; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) { // Bottom to top _calendarTableLayout.removeAllViews(); int month = _calendar.get(Calendar.MONTH); _calendar.set(Calendar.MONTH, month + 1); _calendar.set(Calendar.DATE, 1); // important drawCalendar(); return true; } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) { // Top to bottom _calendarTableLayout.removeAllViews(); int month = _calendar.get(Calendar.MONTH); _calendar.set(Calendar.MONTH, month - 1); _calendar.set(Calendar.DATE, 1); // important redrawCalendar(); return true; } return false; } @Override public void onLongPress(MotionEvent e) { // Do nothing } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // Do nothing return false; } @Override public void onShowPress(MotionEvent e) { // Do nothing } @Override public boolean onSingleTapUp(MotionEvent e) { // Do nothing return false; } } 

Stacktrace:

  01-27 11:12:16.406: E/AndroidRuntime(1448): FATAL EXCEPTION: main 01-27 11:12:16.406: E/AndroidRuntime(1448): java.lang.NullPointerException 01-27 11:12:16.406: E/AndroidRuntime(1448): at android.view.GestureDetector.onTouchEvent(GestureDetector.java:587) 01-27 11:12:16.406: E/AndroidRuntime(1448): at com.my.package.MyActivity$9.onTouch(MyActivity.java:287) 01-27 11:12:16.406: E/AndroidRuntime(1448): at android.view.View.dispatchTouchEvent(View.java:5481) 01-27 11:12:16.406: E/AndroidRuntime(1448): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959) 01-27 11:12:16.406: E/AndroidRuntime(1448): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728) 01-27 11:12:16.406: E/AndroidRuntime(1448): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959) 01-27 11:12:16.406: E/AndroidRuntime(1448): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728) 01-27 11:12:16.406: E/AndroidRuntime(1448): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959) 01-27 11:12:16.406: E/AndroidRuntime(1448): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728) 01-27 11:12:16.406: E/AndroidRuntime(1448): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959) 01-27 11:12:16.406: E/AndroidRuntime(1448): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728) 01-27 11:12:16.406: E/AndroidRuntime(1448): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959) 01-27 11:12:16.406: E/AndroidRuntime(1448): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728) 01-27 11:12:16.406: E/AndroidRuntime(1448): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959) 01-27 11:12:16.406: E/AndroidRuntime(1448): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728) 01-27 11:12:16.406: E/AndroidRuntime(1448): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959) 01-27 11:12:16.406: E/AndroidRuntime(1448): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728) 01-27 11:12:16.406: E/AndroidRuntime(1448): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1892) 01-27 11:12:16.406: E/AndroidRuntime(1448): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1371) 01-27 11:12:16.406: E/AndroidRuntime(1448): at android.app.Activity.dispatchTouchEvent(Activity.java:2364) 01-27 11:12:16.406: E/AndroidRuntime(1448): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java: 1840) 01-27 11:12:16.406: E/AndroidRuntime(1448): at android.view.View.dispatchPointerEvent(View.java:5662) 01-27 11:12:16.406: E/AndroidRuntime(1448): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2863) 01-27 11:12:16.406: E/AndroidRuntime(1448): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442) 01-27 11:12:16.406: E/AndroidRuntime(1448): at android.os.Handler.dispatchMessage(Handler.java:99) 01-27 11:12:16.406: E/AndroidRuntime(1448): at android.os.Looper.loop(Looper.java:137) 01-27 11:12:16.406: E/AndroidRuntime(1448): at android.app.ActivityThread.main(ActivityThread.java:4340) 01-27 11:12:16.406: E/AndroidRuntime(1448): at java.lang.reflect.Method.invokeNative(Native Method) 01-27 11:12:16.406: E/AndroidRuntime(1448): at java.lang.reflect.Method.invoke(Method.java:511) 01-27 11:12:16.406: E/AndroidRuntime(1448): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 01-27 11:12:16.406: E/AndroidRuntime(1448): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 01-27 11:12:16.406: E/AndroidRuntime(1448): at dalvik.system.NativeStart.main(Native Method) 
+4
source share
2 answers

My friend, try returning true when you get the events you want to process from _gestureDetector:

  public boolean onTouch(View v, MotionEvent event) { if (_gestureDetector.onTouchEvent(event)) { return true; } return false; } 

Otherwise, return false.

+3
source

I looked at the Android source (4.0.1 r1) for the GestureDetector: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.1_r1/android/view/ GestureDetector.java?av=f

Line 587 does not look like the source of the problem, since mVelocityTracker is always initialized to this point. Any idea that you built 4.0?

It could be a bug in 4.0, and I would report a bug report to them:

http://code.google.com/p/android/issues/list

At the same time, is it possible to move the code in my OnClickListener to the OnSingleTapConfirmed method for SimpleGestureListener? That way, it should still do the right thing, as if your click listener has been commented out, but you will get the same behavior.

http://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html#onSingleTapConfirmed(android.view.MotionEvent)

0
source

Source: https://habr.com/ru/post/1392759/


All Articles