2

I have an events on swipe left and right on the screen but there is an invalid pointerId=-1 on TouchEvent that on some phones crashes the application here is the class i use

   public class OnSwipeTouchListener implements OnTouchListener {
    private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());
     @Override
     public boolean onTouch(View arg0, MotionEvent arg1) {
    // TODO Auto-generated method stub
    return gestureDetector.onTouchEvent(arg1);
     }
    private final class GestureListener extends SimpleOnGestureListener {

        private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;


        @Override
        public boolean onDown(MotionEvent e) {
         return true;
        }

       @Override
       public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)  {
          boolean result = false;
          try {
              float diffY = e2.getY() - e1.getY();
              float diffX = e2.getX() - e1.getX();

             if (Math.abs(diffX) > Math.abs(diffY)) {
                  if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
              } 
         } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }


}

      public void onSwipeRight() {
      }

      public void onSwipeLeft() {
      }


}

i get this error java.lang.IllegalArgumentException: pointerIndex out of range

in my main activity class i use this

fullView = (View)findViewById(R.id.fullview);
   fullView.setOnTouchListener(imageViewSwiped);  -> inside onCreate method

and after onCreate i use this

     OnTouchListener imageViewSwiped = new OnSwipeTouchListener()
      {
     public void onSwipeRight() {

            minusOne();
        }
        public void onSwipeLeft() {
           // Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show();
            plusOne();
        }
};
denza
  • 1,298
  • 4
  • 24
  • 49

1 Answers1

10

what i did is just commented this override

     /*

    @Override
    public boolean onDown(MotionEvent e) {
     return true;
    }

      */

and everything was perfect again...

denza
  • 1,298
  • 4
  • 24
  • 49
  • This does work. However, tap event is also disabled, so if you use this for e.g. loading bmp's from your own gallery, it's (unfortunately) of no use. I'm in the dark when it comes to understanding why the tap event gives an error, which I can't even catch (Android 9). – Danny E.K. van der Kolk Nov 27 '20 at 15:43
  • I finally had some time to address this issue some more, if you just set 'return false' in your onDown event, it works. The tap is still received, and without error (Huawei P20 lite). And, as a sidenote, I used try/catch in the onDown event, which I should have used in the onTouch event itself (the error is catched there). See https://stackoverflow.com/questions/16459196/java-lang-illegalargumentexception-pointerindex-out-of-range-exception-dispat/ (I tried this and it works, but I don't use it, since I see no error anymore). – Danny E.K. van der Kolk Jan 22 '21 at 00:02