ViewPager: java.lang.IllegalArgumentException: pointerIndex out of range

六眼飞鱼酱① 提交于 2019-12-01 00:36:07

I don't know why MotionEventCompat is calling MotionEventCompatEclair, as I saw in the code, there are a MotionEventCompatHoneycomb overload, but I had the same problem with Moto XT1040 in android 4.4.4.

The solution for me was create my version of ViewPager(I just copied the entire class from android source) and in the method onInterceptTouchEvent(MotionEvent ev) in the case of MotionEvent.ACTION_MOVE change the initial lines from:

final int activePointerId = mActivePointerId;
if (activePointerId == INVALID_POINTER) {
    // If we don't have a valid id, the touch down wasn't on content.
    break;
}

final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
final float x = MotionEventCompat.getX(ev, pointerIndex);
final float dx = x - mLastMotionX;
final float xDiff = Math.abs(dx);
final float y = MotionEventCompat.getY(ev, pointerIndex);
final float yDiff = Math.abs(y - mInitialMotionY);

to:

final int activePointerId = mActivePointerId;
if (activePointerId == INVALID_POINTER) {
    // If we don't have a valid id, the touch down wasn't on content.
    break;
}

final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
final float x = ev.getX(pointerIndex);
final float dx = x - mLastMotionX;
final float xDiff = Math.abs(dx);
final float y = ev.getY(pointerIndex);
final float yDiff = Math.abs(y - mInitialMotionY);

The only change was in the x and y variables, I changed to call the MotionEvent getX and getY method directly, ignoring the MotionEventCompat.

My app was minimum api 14, if you are supporting something previous from 14, I suggest to you call the closest MotionEventCompat from your version. You just need to prevent from calling the MotivoEventCompatEclair version.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!