我是靠谱客的博主 快乐水壶,最近开发中收集的这篇文章主要介绍Android 处理图片放大缩小时报错java.lang.IllegalArgumentException: pointerIndex out of range pointerIndex=-1 ...,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
完整的错误信息:
java.lang.IllegalArgumentException: pointerIndex out of range pointerIndex=-1 pointerCount=1
at android.view.MotionEvent.nativeGetAxisValue(Native Method)
at android.view.MotionEvent.getX(MotionEvent.java:2057)
at android.support.v4.view.MotionEventCompatEclair.getX(MotionEventCompatEclair.java:32)
at android.support.v4.view.MotionEventCompat$EclairMotionEventVersionImpl.getX(MotionEventCompat.java:110)
at android.support.v4.view.MotionEventCompat.getX(MotionEventCompat.java:462)
at android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:1916)
at com.zhuwentao.com.common.component.imageviewer.ImageOriginPager.onInterceptTouchEvent(ImageOriginPager.java:79)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2108)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2555)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2555)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2555)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2555)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2555)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2555)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2555)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2461)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1777)
at android.app.Activity.dispatchTouchEvent(Activity.java:2865)
at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2422)
at android.view.View.dispatchPointerEvent(View.java:9610)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4436)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4302)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3836)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3889)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3855)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3981)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3863)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4038)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3836)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3889)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3855)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3863)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3836)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6135)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6109)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6070)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6251)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:192)
at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:181)
at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:6209)
at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:6282)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:884)
at android.view.Choreographer.doCallbacks(Choreographer.java:696)
at android.view.Choreographer.doFrame(Choreographer.java:625)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:870)
at android.os.Handler.handleCallback(Handler.java:743)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5546)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
错误原因
我这里的这个错误是在继承了ViewPager的类里面,重写的onInterceptTouchEvent方法里产生的,这个异常一旦产生程序就直接崩溃了
为什么pointerIndex的值为-1时就异常停止了呢,顺着源码找到了程序异常终止的地方
在父类的ViewPager中的onTouchEvent方法,处理触摸点移动的位置:
case MotionEvent.ACTION_MOVE:
if (!mIsBeingDragged) {
final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId );
if (pointerIndex == -1 ) {
// A child has consumed some touch events and put us into an inconsistent state.
needsInvalidate = resetTouch();
break;
}
// 省略......
很明显的,当pointerIndex = -1的时候,直接break了,后面的代码都没有被执行到,所以后面的MotionEventCompat.getX等获取触摸点x的值都获取不到了,Android系统这样的处理方式并不友好啊
解决方法
对于这种Android系统级别的bug,最好还是try掉它吧,改动系统源码再编译实在是太麻烦了
在继承至ViewPager的类中,重写 onInterceptTouchEvent 方法,在里面捕获这个异常IllegalArgumentException
public class ImageOriginPager extends ViewPager {
public ImageOriginPager(Context context) {
super (context);
}
public ImageOriginPager(Context context, AttributeSet attrs) {
super (context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
try {
super.onTouchEvent(event);
} catch (IllegalArgumentException e) {
Log.e( "ImageOriginPager-error" , "IllegalArgumentException 错误被活捉了!");
e.printStackTrace();
}
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
try {
super.onInterceptTouchEvent(event);
} catch (IllegalArgumentException e) {
Log.e( "ImageOriginPager-error" , "IllegalArgumentException 错误被活捉了!");
e.printStackTrace();
}
return false ;
}
}
当然也可以在重写的onTouchEvent中也进行一次捕获,双重保护
直接使用ViewPager控件的同学,如果也报IllegalArgumentException错误,可以新建一个自定义的类继承ViewPager,按照上面方式来重写,使用的时候就使用自定义的那个类来替换掉ViewPager
最后
以上就是快乐水壶为你收集整理的Android 处理图片放大缩小时报错java.lang.IllegalArgumentException: pointerIndex out of range pointerIndex=-1 ...的全部内容,希望文章能够帮你解决Android 处理图片放大缩小时报错java.lang.IllegalArgumentException: pointerIndex out of range pointerIndex=-1 ...所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复