我在屏幕上左右滑动了一个事件,但是在touchEvent上有一个无效的pointerID=-1,在某些电话上,这个应用程序崩溃了,这里是我使用的类
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() {
}
}
我得到这个错误java.lang.IllegalargumentException:PointerIndex超出范围
在我的主要活动课上我用这个
fullView = (View)findViewById(R.id.fullview);
fullView.setOnTouchListener(imageViewSwiped); -> inside onCreate method
然后我用这个
OnTouchListener imageViewSwiped = new OnSwipeTouchListener()
{
public void onSwipeRight() {
minusOne();
}
public void onSwipeLeft() {
// Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show();
plusOne();
}
};
最佳答案
我所做的只是评论了这个覆盖
/*
@Override
public boolean onDown(MotionEvent e) {
return true;
}
*/
一切又完美了…
关于android - onSwipe从左到右我得到标签ScrollView和无效的pointerId = -1 onTouchEvent,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20869477/