解决java.lang.IllegalArgumentException: pointerIndex out of range 或者 arrayindexoutofboundsexception的错误

转至:http://blog.csdn.net/com314159/article/details/41245329


因为项目中需要实现一个类似于网易新闻的banner广告的功能, 有多点触摸,有下拉刷新, 两个viewPager放一起了, 导致他们之间的事件冲突, 所以在子的viewPager中实现了


  1. case MotionEvent.ACTION_MOVE:    
  2.     final float xDiff = Math.abs(x - mLastMotionX);  
  3.     final float yDiff = Math.abs(y - mLastMotionY);  
  4.     xDistance += xDiff;  
  5.     yDistance += yDiff;  
  6.       
  7.     /** 左右滑动避免和下拉刷新冲突   **/  
  8.     if (xDistance > yDistance || Math.abs(xDistance - yDistance) < 0.00001f || event.getPointerCount() >= 2) {  
  9.         mIsBeingDragged = true;  
  10.         mLastMotionX =  x;  
  11.         mLastMotionY = y;  
  12.        getParent().requestDisallowInterceptTouchEvent(true);  
  13.     } else {  
  14.         mIsBeingDragged = false;  
  15.         getParent().requestDisallowInterceptTouchEvent(false);  
  16.     }  
  17.       
  18.     break;    


因为调用了requestDisallowInterceptTouchEvent,所以会导致父类的view多点触摸有些情况下会出现数组溢出的情况.

网上的解决多法多为catch这个异常, 这种做法是可以的. 但是我个人是喜欢优雅并且效率高并且易扩展的做法.

就是这种做法

https://code.google.com/p/Android/issues/detail?id=60464


因为我的是父的viewPager冲突了, 当requestDisallowInterceptTouchEvent(true) 时, 简单的理解就是父view无法接收到触摸事件, 多点触摸时就会造成崩溃, 所以即使requestDisallowInterceptTouchEvent(true) 时, 父view也让它去接收事件. 

最终代码如下:

重写会崩溃的父view:

  1. package cn.jaxus.course.common.widget.viewpager;  
  2.   
  3. import android.content.Context;  
  4. import android.support.v4.view.ViewPager;  
  5. import android.util.AttributeSet;  
  6. import android.view.MotionEvent;  
  7.   
  8. /** 
  9.  * @author ZhiCheng Guo 
  10.  * @version 2014年11月18日 下午12:44:59 精品界面多点触摸有bug,会导致pointerIndex out of range的异常, 
  11.  *          所以加了这个view 
  12.  */  
  13. public class MutipleTouchViewPager extends ViewPager {  
  14.   
  15.     public MutipleTouchViewPager(Context context) {  
  16.         super(context);  
  17.     }  
  18.   
  19.     public MutipleTouchViewPager(Context context, AttributeSet attrs) {  
  20.         super(context, attrs);  
  21.     }  
  22.     private boolean mIsDisallowIntercept = false;  
  23.     @Override  
  24.     public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {  
  25.         // keep the info about if the innerViews do  
  26.         // requestDisallowInterceptTouchEvent  
  27.         mIsDisallowIntercept = disallowIntercept;  
  28.         super.requestDisallowInterceptTouchEvent(disallowIntercept);  
  29.     }  
  30.   
  31.     @Override  
  32.     public boolean dispatchTouchEvent(MotionEvent ev) {  
  33.         // the incorrect array size will only happen in the multi-touch  
  34.         // scenario.  
  35.         if (ev.getPointerCount() > 1 && mIsDisallowIntercept) {  
  36.             requestDisallowInterceptTouchEvent(false);  
  37.             boolean handled = super.dispatchTouchEvent(ev);  
  38.             requestDisallowInterceptTouchEvent(true);  
  39.             return handled;  
  40.         } else {  
  41.             return super.dispatchTouchEvent(ev);  
  42.         }  
  43.     }  
  44. }  






利用午休时间写了这篇文章,谢谢大家


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值