图片缩放时java.lang.IllegalArgumentException: pointerIndex out of range解决方案
版权声明:本文为博主原创文章,未经博主允许不得转载。
06-03 20:45:24.143: E/AndroidRuntime(1230): FATAL EXCEPTION: main
06-03 20:45:24.143: E/AndroidRuntime(1230): java.lang.IllegalArgumentException: pointerIndex out of range
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.MotionEvent.nativeGetAxisValue(Native Method)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.MotionEvent.getX(MotionEvent.java:1981)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.support.v4.view.MotionEventCompatEclair.getX(MotionEventCompatEclair.java:32)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.support.v4.view.MotionEventCompat$EclairMotionEventVersionImpl.getX(MotionEventCompat.java:91)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.support.v4.view.MotionEventCompat.getX(MotionEventCompat.java:219)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:1768)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1935)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2289)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2032)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2289)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2032)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2289)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2032)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2289)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2032)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2289)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2032)
06-03 20:45:24.143: E/AndroidRuntime(1230): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2020)
06-03 20:45:24.143: E/AndroidRuntime(1230): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1470)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.app.Activity.dispatchTouchEvent(Activity.java:2471)
06-03 20:45:24.143: E/AndroidRuntime(1230): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1968)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.View.dispatchPointerEvent(View.java:7396)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3251)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3196)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4238)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4217)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4309)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:163)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4288)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4328)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.Choreographer.doFrame(Choreographer.java:523)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.os.Handler.handleCallback(Handler.java:615)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.os.Handler.dispatchMessage(Handler.java:92)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.os.Looper.loop(Looper.java:137)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.app.ActivityThread.main(ActivityThread.java:4803)
06-03 20:45:24.143: E/AndroidRuntime(1230): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 20:45:24.143: E/AndroidRuntime(1230): at java.lang.reflect.Method.invoke(Method.java:511)
06-03 20:45:24.143: E/AndroidRuntime(1230): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-03 20:45:24.143: E/AndroidRuntime(1230): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
06-03 20:45:24.143: E/AndroidRuntime(1230): at dalvik.system.NativeStart.main(Native Method)
06-03 20:45:26.773: E/Trace(1281): error opening trace file: No such file or directory (2)
一个问题,两天时间,搜破了百度没有啥成效。只有这点东西,到处转的,没有说明怎么用。帮助不大!百度上仅有的点内容如下:
=========================================================================================================
在做多点触控放大缩小,操作自己所绘制的图形时发生这个异常,如果是操作图片的放大缩小多点触控不会出现这个错误
这个bug是Android系统原因 所以第一种方式是:
修改frameworks\base\core\jni\android_view_MotionEvent.cpp的android_view_MotionEvent_nativeGetAxisValue方法
注释掉
[java]
if (!validatePointerIndex(env, pointerIndex, pointerCount)) {return 0;}
改完后需重新编译整个系统,然后替换lib库,重新编译整个系统一般需要半个多小时,这个方法就比较麻烦了
第二种方法是:捕获IllegalArgumentException(非法参数异常)异常 即如
[java]
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();
}
第二种方法简单有效
========================================================================================================
这种方法我不会用,不知道该用在哪儿。
接着用谷歌搜索,顺着终于找到了解决方案。经历了以下网站:
http://stackoverflow.com/questions/6919292/pointerindex-out-of-range-android-multitouch
https://code.google.com/p/android/issues/detail?id=18990
https://github.com/chrisbanes/PhotoView/issues/31
上面是搜索的过程,现在把精华内容拣出来:思路是自定义ViewPager,重写onTouchEvent 和onInterceptTouchEvent。具体见下:
It's the android ViewPager's bug
stackoverflow's report: http://stackoverflow.com/questions/6919292/pointerindex-out-of-range-android-multitouch
android's report: http://code.google.com/p/android/issues/detail?id=18990
My simple's method to fix this bug:
You can extends the ViewPager class, your own ViewPager should override the onTouchEvent and the onInterceptTouchEvent methods, and try-catch the IllegalArgumentException exception. Then use your own ViewPager class in layout or others you want.
Examples:
- /** Custom your own ViewPager to extends support ViewPager. java source: */
- /** Created by azi on 2013-6-21. */
- package com.chaokuadi.android.support.view;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.view.MotionEvent;
- public class ViewPagerFixed extends android.support.v4.view.ViewPager {
- public ViewPagerFixed(Context context) {
- super(context);
- }
- public ViewPagerFixed(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- @Override
- public boolean onTouchEvent(MotionEvent ev) {
- try {
- return super.onTouchEvent(ev);
- } catch (IllegalArgumentException ex) {
- ex.printStackTrace();
- }
- return false;
- }
- @Override
- public boolean onInterceptTouchEvent(MotionEvent ev) {
- try {
- return super.onInterceptTouchEvent(ev);
- } catch (IllegalArgumentException ex) {
- ex.printStackTrace();
- }
- return false;
- }
- }
- /** ViewPager layout */
- <?xml version="1.0" encoding="utf-8"?>
- <com.chaokuaidi.android.support.view.ViewPagerFixed xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/pager"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" />