网站开发检测用户微信号,开发一款游戏,义乌注册公司流程和费用,wordpress常见插件问题描述
错误 AndroidRuntime: java.lang.IllegalArgumentException: invalid pointerIndex -1 for MotionEvent 表示由于无效的指针索引导致 Android 应用程序崩溃MotionEvent目的。此错误通常发生在应用处理触摸事件时#xff0c;尤其是使用ViewPager在不同的视图或页面之…问题描述
错误 AndroidRuntime: java.lang.IllegalArgumentException: invalid pointerIndex -1 for MotionEvent 表示由于无效的指针索引导致 Android 应用程序崩溃MotionEvent目的。此错误通常发生在应用处理触摸事件时尤其是使用ViewPager在不同的视图或页面之间滑动。
关于此报错的 Google Issue Tracker : https://issuetracker.google.com/issues/308367897
具体日志
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: FATAL EXCEPTION: main
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: Process: com.xxx.calculator, PID: 20502
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: java.lang.IllegalArgumentException: invalid pointerIndex -1 for MotionEvent { actionMOVE, id[0]1, x[0]2252.04, y[0]985.088, historySize2, eventTime1273843185000, downTime1273305149000, deviceId4, sourceTOUCHSCREEN, displayId0, eventId552843928}
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.MotionEvent.nativeGetAxisValue(Native Method)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.MotionEvent.getX(MotionEvent.java:2460)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at androidx.viewpager.widget.ViewPager.onInterceptTouchEvent(Unknown Source:56)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2664)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3178)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2824)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3178)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2824)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3178)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2824)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3178)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2824)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:511)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1899)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.app.Activity.dispatchTouchEvent(Activity.java:4278)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:469)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.View.dispatchPointerEvent(View.java:15320)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:6763)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:6544)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5996)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:6053)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:6019)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:6184)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:6027)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:6241)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6000)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:6053)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:6019)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:6027)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6000)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:9154)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:9105)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:9060)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:9302)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:267)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:247)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:9239)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:9402)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1315)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1323)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:956)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:851)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1298)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:942)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.os.Looper.loopOnce(Looper.java:201)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.os.Looper.loop(Looper.java:288)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7963)
05-24 09:56:28.218584 20502 20502 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
05-24 09:56:28.218621 20502 20502 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:569)
05-24 09:56:28.218621 20502 20502 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1034)Android ViewPager 中的拦截所有触摸屏动作实现 onInterceptTouchEvent
Override
public boolean onInterceptTouchEvent(MotionEvent ev) {/** This method JUST determines whether we want to intercept the motion.* If we return true, onMotionEvent will be called and we do the actual* scrolling there.*/final int action ev.getAction() MotionEvent.ACTION_MASK;// Always take care of the touch gesture being complete.if (action MotionEvent.ACTION_CANCEL || action MotionEvent.ACTION_UP) {// Release the drag.if (DEBUG) Log.v(TAG, Intercept done!);resetTouch();return false;}// Nothing more to do here if we have decided whether or not we// are dragging.if (action ! MotionEvent.ACTION_DOWN) {if (mIsBeingDragged) {if (DEBUG) Log.v(TAG, Intercept returning true!);return true;}if (mIsUnableToDrag) {if (DEBUG) Log.v(TAG, Intercept returning false!);return false;}}switch (action) {case MotionEvent.ACTION_MOVE: {/** mIsBeingDragged false, otherwise the shortcut would have caught it. Check* whether the user has moved far enough from his original down touch.*//** Locally do absolute value. mLastMotionY is set to the y value* of the down event.*/final int activePointerId mActivePointerId;if (activePointerId INVALID_POINTER) {// If we dont have a valid id, the touch down wasnt on content.break;}final int pointerIndex ev.findPointerIndex(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);if (DEBUG) Log.v(TAG, Moved x to x , y diff xDiff , yDiff);if (dx ! 0 !isGutterDrag(mLastMotionX, dx) canScroll(this, false, (int) dx, (int) x, (int) y)) {// Nested view has scrollable area under this point. Let it be handled there.mLastMotionX x;mLastMotionY y;mIsUnableToDrag true;return false;}if (xDiff mTouchSlop xDiff * 0.5f yDiff) {if (DEBUG) Log.v(TAG, Starting drag!);mIsBeingDragged true;requestParentDisallowInterceptTouchEvent(true);setScrollState(SCROLL_STATE_DRAGGING);mLastMotionX dx 0? mInitialMotionX mTouchSlop : mInitialMotionX - mTouchSlop;mLastMotionY y;setScrollingCacheEnabled(true);} else if (yDiff mTouchSlop) {// The finger has moved enough in the vertical// direction to be counted as a drag... abort// any attempt to drag horizontally, to work correctly// with children that have scrolling containers.if (DEBUG) Log.v(TAG, Starting unable to drag!);mIsUnableToDrag true;}if (mIsBeingDragged) {// Scroll to follow the motion eventif (performDrag(x)) {ViewCompat.postInvalidateOnAnimation(this);}}break;}case MotionEvent.ACTION_DOWN: {/** Remember location of down touch.* ACTION_DOWN always refers to pointer index 0.*/mLastMotionX mInitialMotionX ev.getX();mLastMotionY mInitialMotionY ev.getY();mActivePointerId ev.getPointerId(0);mIsUnableToDrag false;mIsScrollStarted true;mScroller.computeScrollOffset();if (mScrollState SCROLL_STATE_SETTLING Math.abs(mScroller.getFinalX() - mScroller.getCurrX()) mCloseEnough) {// Let the user catch the pager as it animates.mScroller.abortAnimation();mPopulatePending false;populate();mIsBeingDragged true;requestParentDisallowInterceptTouchEvent(true);setScrollState(SCROLL_STATE_DRAGGING);} else {completeScroll(false);mIsBeingDragged false;}if (DEBUG) {Log.v(TAG, Down at mLastMotionX , mLastMotionY mIsBeingDragged mIsBeingDragged mIsUnableToDrag mIsUnableToDrag);}break;}case MotionEvent.ACTION_POINTER_UP:onSecondaryPointerUp(ev);break;}if (mVelocityTracker null) {mVelocityTracker VelocityTracker.obtain();}mVelocityTracker.addMovement(ev);/** The only time we want to intercept motion events is if we are in the* drag mode.*/return mIsBeingDragged;
}根据Log信息定位报错的问题点
final float x ev.getX(pointerIndex);修改方案
捕获IllegalArgumentException非法参数异常异常
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;import androidx.viewpager.widget.ViewPager;public class FixedViewPager extends ViewPager {public FixedViewPager(Context context) {super(context);}public FixedViewPager(Context context, AttributeSet attrs) {super(context, attrs);}Overridepublic boolean onTouchEvent(MotionEvent ev) {try {return super.onTouchEvent(ev);} catch (Exception ex) {ex.printStackTrace();}return false;}Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {try {return super.onInterceptTouchEvent(ev);} catch (Exception ex) {ex.printStackTrace();}return false;}
}