在记事帐的应用后台收到了一条罕见的崩溃报告:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
java.lang.IllegalArgumentException: pointerIndex out of range at android.view.MotionEvent.nativeGetAxisValue(Native Method) at android.view.MotionEvent.getX(MotionEvent.java:2189) at android.support.v4.view.MotionEventCompatEclair.getX(MotionEventCompatEclair.java:32) at android.support.v4.view.MotionEventCompat$EclairMotionEventVersionImpl.getX(MotionEventCompat.java:86) at android.support.v4.view.MotionEventCompat.getX(MotionEventCompat.java:184) at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:1462) at android.view.View.dispatchTouchEvent(View.java:8149) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2140) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2155) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2155) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2155) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2155) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2155) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2155) at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2295) at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1622) at android.app.Activity.dispatchTouchEvent(Activity.java:2565) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2243) at android.view.View.dispatchPointerEvent(View.java:8357) at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4778) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4644) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4202) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4256) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4225) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4336) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4233) at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4393) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4202) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4256) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4225) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4233) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4202) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6569) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6486) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6457) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6422) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6649) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:138) at android.os.Looper.loop(Looper.java:131) at android.app.ActivityThread.main(ActivityThread.java:5602) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) at dalvik.system.NativeStart.main(Native Method) |
经调查,发现这是 Android 的 ViewPager 的一个 bug,偶尔会在用户执行多点触控操作时发生。直到至少 Android 4.4,该问题依然存在。
解决方案也很简单,覆写 ViewPager 的 onTouchEvent 与 onInterceptTouchEvent 方法,catch 该异常即可,如下所示:
1 2 3 4 5 6 7 8 9 |
@Override public boolean onTouchEvent(MotionEvent me) { try { return super.onTouchEvent(me); } catch (IllegalArgumentException ex) { ex.printStackTrace(); } return false; } |
《Android 中 java.lang.IllegalArgumentException: pointerIndex out of range 异常的处理方法》有一个想法