ViewPager + SubsamplingScaleImageView 引起的 IllegalArgumentException

  • ViewPager,SubsamplingScaleImageView,IllegalArgumentException,多点触摸
  • 2020.09.22

最近收到一些崩溃日志,大概如下:

java.lang.IllegalArgumentException: pointerIndex out of range
at android.view.MotionEvent.nativeGetAxisValue(Native Method)
at android.view.MotionEvent.getX(MotionEvent.java:2122)
at android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:2066)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2190)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2663)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2336)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2663)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2336)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2663)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2336)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2663)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2336)
at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:527)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1850)
at android.app.Activity.dispatchTouchEvent(Activity.java:3165)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:482)
at android.view.View.dispatchPointerEvent(View.java:10292)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4498)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4354)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3867)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3937)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3895)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4037)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3903)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4094)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3867)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3937)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3895)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3903)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3867)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6321)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6295)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6250)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6431)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:193)
at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:180)
at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:6402)
at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:6454)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:908)
at android.view.Choreographer.doCallbacks(Choreographer.java:720)
at android.view.Choreographer.doFrame(Choreographer.java:649)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:894)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6292)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:906)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)

重点在 android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:2066) 这行日志上。在我们的项目中,在一个 ViewPager 上嵌套使用了 SubsamplingScaleImageView 来实现大图的浏览,在用户实际使用过程中,偶尔会采集到一些崩溃日志。大概原因是 ViewPager 在处理多点触摸的时候,事件分发出现了异常,网上的大部分解决方案是重写一个 ViewPager ,在 onInterceptTouchEvent 方法做一下 try catch,简单粗暴。

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
   try {
       return super.onInterceptTouchEvent(ev);
   } catch (IllegalArgumentException ex) {
       ex.printStackTrace();
   }
   return false;
}

相关文章

- EOF -

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处,尊重他人劳动。
转载请注明:文章转载自 Binkery 技术博客 [https://binkery.com]
本文标题: ViewPager + SubsamplingScaleImageView 引起的 IllegalArgumentException
本文地址: https://binkery.com/archives/2020.09.22-viewpager-subsamplingscaleimageview.html