2015-03-25 143 views
6

我有viewpager與fragments.when我試圖移動viewpager它工作正常。但如果我快速刷卡它給我下面的異常。ViewPager快速刷卡給IndexOutOfBoundsException

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 
+0

您是否嘗試過加入viewPager.setOffscreenPageLimit( X);其中x是您在尋呼機中的總頁數? – 2015-03-25 03:17:05

+0

看起來像你的apdater沒有網頁。你有物品到適配器嗎?粘貼代碼。 – Bharatesh 2015-03-25 03:25:04

回答

3

擴展ViewPager和覆蓋onTouchEventonInterceptTouchEvent忽略在當前項目和ViewPager的孩子數是以下的情況下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); 
}