IllegalArgumentException: pointerIndex out of range pointerIndex=-1 pointerCount=1

使用PhotoView和ViewPager组合是出问题,具体错误如下

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:2122)
                                                                      at android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:2064)
                                                                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2212)
                                                                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2671)
                                                                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
                                                                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2671)
                                                                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
                                                                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2671)
                                                                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
                                                                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2671)
                                                                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
                                                                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2671)
                                                                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
                                                                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2671)
                                                                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
                                                                      at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:447)
                                                                      at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1871)
                                                                      at android.app.Activity.dispatchTouchEvent(Activity.java:3213)
                                                                      at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
                                                                      at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:409)
                                                                      at android.view.View.dispatchPointerEvent(View.java:10232)


报错是在ViewPager的onInterceptTouchEvent方法中,网上也有查此类错误的解决,说是:

=========================

修改frameworks\base\core\jni\android_view_MotionEvent.cpp的android_view_MotionEvent_nativeGetAxisValue方法

注释掉
[java]  view plain  copy
  1. if (!validatePointerIndex(env, pointerIndex, pointerCount))   
  2. {return 0;}  


     改完后需重新编译整个系统,然后替换lib库,重新编译整个系统一般需要半个多小时,这个方法就比较麻烦了
 
第二种方法是:捕获IllegalArgumentException(非法参数异常)异常 即如

==============

上为引文出自:https://blog.csdn.net/eoeandroida/article/details/7954398 点击打开链接

于是乎,自定义View继承ViewPager重写onInterceptTouchEvent方法捕获异常,确实解决了问题:

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

你可能感兴趣的:(控件使用,自定义View)