PhotoView+ViewPager抛出pointerIndex out of range异常的解决方法

Android PhotoView+ViewPager 发生java.lang.IllegalArgumentException: pointerIndex out of range异常 的解决方法

问题描述

当PhotoView 和 ViewPager 组合时 ,用双指进行放大时 是没有问题的,但是用双指进行缩小的时候,程序就会崩掉,并且抛出java.lang.IllegalArgumentException: pointerIndex out of range

异常详细信息

        java.lang.IllegalArgumentException: pointerIndex out of range
        at android.view.MotionEvent.nativeGetAxisValue(Native Method)
        at android.view.MotionEvent.getX(MotionEvent.java:1979)
        at android.support.v4.view.MotionEventCompatEclair.getX(MotionEventCompatEclair.java:32)
        at android.support.v4.view.MotionEventCompat$EclairMotionEventVersionImpl.getX(MotionEventCompat.java:116)
        at android.support.v4.view.MotionEventCompat.getX(MotionEventCompat.java:496)
        at android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:1977)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1913)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2348)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2042)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2348)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2042)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2348)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2042)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2348)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2042)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2164)
        at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1596)
        at android.app.Dialog.dispatchTouchEvent(Dialog.java:757)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2112)
        at android.view.View.dispatchPointerEvent(View.java:8015)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4632)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4503)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4023)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4073)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4042)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4149)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4050)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4206)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4023)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4073)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4042)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4050)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4023)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6331)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6293)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6247)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6479)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
        at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
        at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
        at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:6441)
        at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:6507)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
        at android.view.Choreographer.doCallbacks(Choreographer.java:591)
        at android.view.Choreographer.doFrame(Choreographer.java:558)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
        at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5386)
        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:829)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
        at dalvik.system.NativeStart.main(Native Method)

解决方法:

只需要自定义一个类去继承ViewPager ,然后重写Viewpager的 onInterceptTouchEvent()的方法

代码:

    package cc.coach.bodyplus.widget;

    import android.content.Context;
    import android.support.v4.view.ViewPager;
    import android.util.AttributeSet;
    import android.view.MotionEvent;

    /** * Created by Bodyplus on 2016/4/18. * <p/> * <p/> * 解决 photoview 与viewpager 组合时 图片缩放的错误 ;异常:.IllegalArgumentException: pointerIndex out of range */
    public class BigPhotoViewPager extends ViewPager {


        public BigPhotoViewPager(Context context) {
            super(context);
        }

        public BigPhotoViewPager(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

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

    }

你可能感兴趣的:(exception,android,viewpager)