我收到以下异常
Exception java.lang.IllegalArgumentException: pointerIndex out of range
android.view.MotionEvent.nativeGetAxisValue (MotionEvent.java)
android.view.MotionEvent.getX (MotionEvent.java:2141)
android.support.v4.view.ViewPager.onInterceptTouchEvent
(ViewPager.java:2092)
android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2254)
android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2711)
android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2400)
android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2711)
android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2400)
android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2711)
android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2400)
com.android.internal.policy.DecorView.superDispatchTouchEvent
(DecorView.java:416)
com.android.internal.policy.PhoneWindow.superDispatchTouchEvent
(PhoneWindow.java:1837)
android.app.Activity.dispatchTouchEvent (Activity.java:3154)
com.android.internal.policy.DecorView.dispatchTouchEvent
(DecorView.java:378)
android.view.View.dispatchPointerEvent (View.java:10177)
android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent
(ViewRootImpl.java:4634)
android.view.ViewRootImpl$ViewPostImeInputStage.onProcess
(ViewRootImpl.java:4502)
android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3953)
android.view.ViewRootImpl$InputStage.onDeliverToNext
(ViewRootImpl.java:4006)
android.view.ViewRootImpl$InputStage.forward (ViewRootImpl.java:3972)
android.view.ViewRootImpl$InputStage.apply (ViewRootImpl.java:3980)
android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3953)
android.view.ViewRootImpl$InputStage.onDeliverToNext
(ViewRootImpl.java:4006)
android.view.ViewRootImpl$InputStage.forward (ViewRootImpl.java:3972)
android.view.ViewRootImpl$AsyncInputStage.forward (ViewRootImpl.java:4101)
android.view.ViewRootImpl$InputStage.apply (ViewRootImpl.java:3980)
android.view.ViewRootImpl$AsyncInputStage.apply (ViewRootImpl.java:4158)
android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3953)
android.view.ViewRootImpl$InputStage.onDeliverToNext
(ViewRootImpl.java:4006)
android.view.ViewRootImpl$InputStage.forward (ViewRootImpl.java:3972)
android.view.ViewRootImpl$InputStage.apply (ViewRootImpl.java:3980)
android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3953)
android.view.ViewRootImpl.deliverInputEvent (ViewRootImpl.java:6443)
android.view.ViewRootImpl.doProcessInputEvents (ViewRootImpl.java:6417)
android.view.ViewRootImpl.enqueueInputEvent (ViewRootImpl.java:6378)
android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent
(ViewRootImpl.java:6577)
android.view.InputEventReceiver.dispatchInputEvent
(InputEventReceiver.java:185)
android.view.InputEventReceiver.nativeConsumeBatchedInputEvents
(InputEventReceiver.java)
android.view.InputEventReceiver.consumeBatchedInputEvents
(InputEventReceiver.java:176)
android.view.ViewRootImpl.doConsumeBatchedInput (ViewRootImpl.java:6525)
android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run
(ViewRootImpl.java:6600)
android.view.Choreographer$CallbackRecord.run (Choreographer.java:871)
android.view.Choreographer.doCallbacks (Choreographer.java:683)
android.view.Choreographer.doFrame (Choreographer.java:613)
android.view.Choreographer$FrameDisplayEventReceiver.run
(Choreographer.java:857)
android.os.Handler.handleCallback (Handler.java:751)
android.os.Handler.dispatchMessage (Handler.java:95)
android.os.Looper.loop (Looper.java:154)
android.app.ActivityThread.main (ActivityThread.java:6310)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:872)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:762)
我在应用程序中有2个viewpagers。两者都是自定义的-一个在onInterceptTouchEvent中返回false。另一个在onInterceptTouchEvent中实现了try catch。那么该错误是怎么发生的呢?
为下面的2个viewpagers添加了代码
public class BottomBarViewPager extends ViewPager {
public BottomBarViewPager(Context context) {
super(context);
}
public BottomBarViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return false;
}
}
public class GalleryViewPager extends ViewPager {
public static final String TAG = GalleryViewPager.class.getSimpleName();
private boolean swipeEnabled;
public GalleryViewPager(Context context, AttributeSet attributes) {
super(context, attributes);
this.swipeEnabled = true;
}
@Override
public boolean onTouchEvent(MotionEvent te) {
if (this.swipeEnabled) {
return super.onTouchEvent(te);
}
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent me) {
if (this.swipeEnabled) {
/*
This is caused by a bug in ViewPager and a solution is to catch the Exception
and continue on. This only happens if you quickly tap with 2 fingers in different
locations, rapidly.
*/
try {
return super.onInterceptTouchEvent(me);
} catch (IllegalArgumentException ex) {
// SinclairLogger.e(TAG, "Caught exception in onInterceptTouchEvent", ex);
}
}
return false;
}
public void setPagingEnabled(boolean enabled) {
this.swipeEnabled = enabled;
}
}
根据要求为viewpagers添加了代码
最佳答案
我有同样的问题(感谢azi)
使用ViewPagerFixed修复了您的错误
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;
}}
您的xml文件
<com.radesh.android.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" />
BottomBarViewPager可以扩展到ViewPagerFixed
关于android - Viewpager-尝试处理捕获,但使指针索引超出范围,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48496257/