ViewPager fast swipes gives IndexOutOfBoundsException

99封情书 提交于 2021-02-07 06:26:25

问题


I have viewpager with fragments.when I try to move viewpager it works fine.but if I swipe fast it gives me below exception.

03-25 11:01:44.901: E/AndroidRuntime(20880): FATAL EXCEPTION: main
03-25 11:01:44.901: E/AndroidRuntime(20880): Process: com.sph.wanbaoandroid, PID: 20880
03-25 11:01:44.901: E/AndroidRuntime(20880): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
03-25 11:01:44.901: E/AndroidRuntime(20880):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at java.util.ArrayList.get(ArrayList.java:308)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.support.v4.view.ViewPager.performDrag(ViewPager.java:2076)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:1999)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at com.sph.wb.pdf.controller.ViewPagerWithTapGestreDetection.onTouchEvent(ViewPagerWithTapGestreDetection.java:42)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.View.dispatchTouchEvent(View.java:8281)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2471)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2194)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2477)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2209)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2477)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2209)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2477)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2209)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2477)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2209)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2477)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2209)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2332)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1616)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.app.Activity.dispatchTouchEvent(Activity.java:2665)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2280)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.View.dispatchPointerEvent(View.java:8489)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4890)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4741)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4295)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4349)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4318)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4429)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4326)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4486)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4295)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4349)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4318)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4326)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4295)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6705)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6584)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6555)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6520)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6785)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:6758)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:6804)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:813)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.Choreographer.doCallbacks(Choreographer.java:613)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.Choreographer.doFrame(Choreographer.java:581)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:799)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.os.Handler.handleCallback(Handler.java:733)
03-25 11:01:44.901: E/AndroidRuntime(20880):    at android.os.Handler.dispatchMessage(Han

回答1:


Extend ViewPager and override onTouchEvent and onInterceptTouchEvent to ignore the case where the the current item and the child count of the ViewPager are both 0.

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (getCurrentItem() == 0 && getChildCount() == 0) {
        return false;
    }

    return super.onTouchEvent(ev);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (getCurrentItem() == 0 && getChildCount() == 0) {
        return false;
    }

    return super.onInterceptTouchEvent(ev);
}


来源:https://stackoverflow.com/questions/29246783/viewpager-fast-swipes-gives-indexoutofboundsexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!