bug_ _ java.lang.IllegalArgumentException: pointerIndex out of range 问题的两种解决办法
========== 4 如何解决java.lang.IllegalArgumentException: pointerIndex out of range?
今天遇到一个bug:java.lang.IllegalArgumentException: pointerIndex out of range
at android.view.MotionEvent.nativeGetAxisValue(Native Method)
at android.view.MotionEvent.getX(MotionEvent.java:1981)
at android.support.v4.view.MotionEventCompatEclair.getX(MotionEventCompatEclair.java:32)
at android.support.v4.view.MotionEventCompat$EclairMotionEventVersionImpl.getX(MotionEventCompat.java:91)
at android.support.v4.view.MotionEventCompat.getX(MotionEventCompat.java:219)
at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:1901)
at cn.xs8.app.activity.news.ui.CustomViewPager.onTouchEvent(CustomViewPager.java:29)
at android.view.View.dispatchTouchEvent(View.java:7263)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1932)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1977)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1427)
at android.app.Activity.dispatchTouchEvent(Activity.java:2428)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1925)
at android.view.View.dispatchPointerEvent(View.java:7443)
at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3603)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3531)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4783)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4743)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4895)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:171)
at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4863)
at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4917)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
at android.view.Choreographer.doCallbacks(Choreographer.java:579)
at android.view.Choreographer.doFrame(Choreographer.java:546)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
在网上很多关于该bug的处理都众说纷纭,我在这里不再详述该bug产生的原因,下面给出一种在4.1的机子上测试通过的方案:
1.让你的view(可能是ScrollView,WebView,MapView等),创建一个子view继承于它们中的某一个;
2.重写onInterceptTouchEvent 和onTouchEvent方法
3.try catch 该两个方法,形如下面:
try{
super.onInterceptTouchEvent(MotionEvent ev)
} catch(ILLegalArgumentException ex) {
}
return false;
try{
super.onTouchEvent(MotionEvent ev)
} catch(ILLegalArgumentException ex) {
}
return false;
========= 3
在ViewPager里缩放图片过小出的异常,自己看了网上的方法:
1.让你的view(可能是ScrollView,WebView,MapView等),创建一个子view继承于它们中的某一个;
2.重写onInterceptTouchEvent 和onTouchEvent方法
3.try catch 该两个方法,形如下面:
try{
super.onInterceptTouchEvent(MotionEvent ev)
} catch(ILLegalArgumentException ex) {
}
return false;
try{
super.onTouchEvent(MotionEvent ev)
} catch(ILLegalArgumentException ex) {
}
return false;
Tips:但是有点问题,如果是用ViewPager的话,onInterceptTouchEvent 返回false会导致ViewPager翻页出现BUG
- import android.content.Context;
- import android.support.v4.view.ViewPager;
- import android.util.AttributeSet;
- import android.view.MotionEvent;
- /**
- * ViewPager类,(此类只是为了防止双指缩放过小而出异常)
- * @author
- *
- */
- public class ImageViewPager extends ViewPager {
- public ImageViewPager(Context context) {
- super(context);
- }
- public ImageViewPager(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- @Override
- public boolean onInterceptTouchEvent(MotionEvent arg0) {
- boolean b = false;
- try {
- b = super.onInterceptTouchEvent(arg0);
- } catch (Exception e) {
- }
- return b; //网上看的方法是直接返回false,但是会导致ViewPager翻页有BUG
- }
- @Override
- public boolean onTouchEvent(MotionEvent arg0) {
- try {
- super.onTouchEvent(arg0);
- } catch (Exception e) {
- }
- return false;
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- >
- <!-- ViewPager用自己定义的 -->
- <com.ghl.components.ImageViewPager
- android:id="@+id/show_image_viewpager"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#4000"
- />
- <include layout="@layout/image_selector_titlebar"/>
- <include layout="@layout/image_selector_bottom"/>
- </RelativeLayout>
我是这样处理ViewPager翻页就没问题了。
======= 2
转载请注明:http://blog.csdn.net/aaawqqq
Android java.lang.IllegalArgumentException: pointerIndex out of range
bug:
- 11-27 00:12:55.029: E/AndroidRuntime(22447): java.lang.IllegalArgumentException: pointerIndex out of range
- 11-27 00:12:55.029: E/AndroidRuntime(22447): at android.view.MotionEvent.nativeGetAxisValue(Native Method)
原因
绘图的时候 多点触控 放大缩小 导致一场 , 我在viewpager 里面 通过手势绘制图片 放大缩小 造成了这个bug
1 继承 view
2 重写 dispatchTouchEvent
3 抓住异常
- public class FixedViewPager extends ViewPager {
- public FixedViewPager(Context context) {
- super(context);
- }
- public FixedViewPager(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- @Override
- public boolean dispatchTouchEvent(MotionEvent ev) {
- try {
- return super .dispatchTouchEvent(ev);
- } catch (IllegalArgumentException ignored) {
- } catch (ArrayIndexOutOfBoundsException e) {
- }
- return false ;
- }
- }
网上有其它方法 如:
重写onInterceptTouchEvent 和onTouchEvent方法
try catch 该两个方法,形如下面:
- try{
- super.onInterceptTouchEvent(MotionEvent ev)
- } catch(ILLegalArgumentException ex) {
- }
- return false;
- try{
- super.onTouchEvent(MotionEvent ev)
- } catch(ILLegalArgumentException ex) {
- }
- return false;
参考: http://nanjingjiangbiao-t.iteye.com/blog/1794478
============= 1
在做多点触控放大缩小,操作自己所绘制的图形时发生这个异常, 如果是操作图片的放大缩小多点触控不会出现这个错误
这个bug是Android系统原因 所以第一种方式是:
修改frameworks\base\core\jni\android_view_MotionEvent.cpp的 android_view_MotionEvent_nativeGetAxisValue方法
- if (!validatePointerIndex(env, pointerIndex, pointerCount))
- {return 0;}
- private float spacing(MotionEvent event) {
- try {
- x = event.getX(0) - event.getX(1);
- y = event.getY(0) - event.getY(1);
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }