解决java.lang.IllegalArgumentException: pointerIndex out of range 或者 arrayindexoutofboundsexception的错误
来源:互联网 发布:unix高级环境编程 编辑:程序博客网 时间:2024/05/22 18:36
因为项目中需要实现一个类似于网易新闻的banner广告的功能, 有多点触摸,有下拉刷新, 两个viewPager放一起了, 导致他们之间的事件冲突, 所以在子的viewPager中实现了
case MotionEvent.ACTION_MOVE: final float xDiff = Math.abs(x - mLastMotionX); final float yDiff = Math.abs(y - mLastMotionY); xDistance += xDiff; yDistance += yDiff; /** 左右滑动避免和下拉刷新冲突 **/ if (xDistance > yDistance || Math.abs(xDistance - yDistance) < 0.00001f || event.getPointerCount() >= 2) { mIsBeingDragged = true; mLastMotionX = x; mLastMotionY = y; getParent().requestDisallowInterceptTouchEvent(true); } else { mIsBeingDragged = false; getParent().requestDisallowInterceptTouchEvent(false); } break;
因为调用了requestDisallowInterceptTouchEvent,所以会导致父类的view多点触摸有些情况下会出现数组溢出的情况.
网上的解决多法多为catch这个异常, 这种做法是可以的. 但是我个人是喜欢优雅并且效率高并且易扩展的做法.
就是这种做法
https://code.google.com/p/android/issues/detail?id=60464
因为我的是父的viewPager冲突了, 当requestDisallowInterceptTouchEvent(true) 时, 简单的理解就是父view无法接收到触摸事件, 多点触摸时就会造成崩溃, 所以即使requestDisallowInterceptTouchEvent(true) 时, 父view也让它去接收事件.
最终代码如下:
重写会崩溃的父view:
package cn.jaxus.course.common.widget.viewpager;import android.content.Context;import android.support.v4.view.ViewPager;import android.util.AttributeSet;import android.view.MotionEvent;/** * @author ZhiCheng Guo * @version 2014年11月18日 下午12:44:59 精品界面多点触摸有bug,会导致pointerIndex out of range的异常, * 所以加了这个view */public class MutipleTouchViewPager extends ViewPager {public MutipleTouchViewPager(Context context) {super(context);}public MutipleTouchViewPager(Context context, AttributeSet attrs) {super(context, attrs);}private boolean mIsDisallowIntercept = false;@Overridepublic void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {// keep the info about if the innerViews do// requestDisallowInterceptTouchEventmIsDisallowIntercept = disallowIntercept;super.requestDisallowInterceptTouchEvent(disallowIntercept);}@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {// the incorrect array size will only happen in the multi-touch// scenario.if (ev.getPointerCount() > 1 && mIsDisallowIntercept) {requestDisallowInterceptTouchEvent(false);boolean handled = super.dispatchTouchEvent(ev);requestDisallowInterceptTouchEvent(true);return handled;} else {return super.dispatchTouchEvent(ev);}}}
利用午休时间写了这篇文章,谢谢大家
1 0
- 解决java.lang.IllegalArgumentException: pointerIndex out of range 或者 arrayindexoutofboundsexception的错误
- 解决java.lang.IllegalArgumentException: pointerIndex out of range 或者 arrayindexoutofboundsexception的错误
- 解决java.lang.IllegalArgumentException: pointerIndex out of range的错误
- 解决java.lang.IllegalArgumentException: pointerIndex out of range的错误
- 完美解决java.lang.IllegalArgumentException: pointerIndex out of range
- java.lang.IllegalArgumentException: pointerIndex out of range
- java.lang.IllegalArgumentException: pointerIndex out of range
- java.lang.IllegalArgumentException: pointerIndex out of range
- java.lang.IllegalArgumentException: pointerIndex out of range
- java.lang.IllegalArgumentException: pointerIndex out of range
- java.lang.IllegalArgumentException: pointerIndex out of range
- java.lang.IllegalArgumentException pointerIndex out of range
- java.lang.IllegalArgumentException: pointerIndex out of range
- java.lang.IllegalArgumentException: pointerIndex out of range两种解决办法
- 图片缩放时java.lang.IllegalArgumentException: pointerIndex out of range
- Android java.lang.IllegalArgumentException: pointerIndex out of range
- 使用PohtoView出现java.lang.IllegalArgumentException: pointerIndex out of range
- 解决图片缩放时异常:java.lang.IllegalArgumentException: pointerIndex out of range
- GIT和SVN之间的五个基本区别
- 第12周 项目4-银行系统(任务2)
- 【WebGL初学系列之四】纹理三角
- windows下忘记mysql超级管理员root密码的解决办法
- java Future 接口介绍
- 解决java.lang.IllegalArgumentException: pointerIndex out of range 或者 arrayindexoutofboundsexception的错误
- window下github上传markdown文件中文乱码
- 10款sublime插件推荐
- Swift学习之UI开发初探
- 第三章 栈和队列
- 高精度加剪乘
- 做几个leetcode数组题
- iOS:一个Cell中设置另外一个Cell中的button
- 如何通过RamDisk的方法加速小型数据库的访问速度