简体   繁体   中英

ViewPager crashes when fragment with webview is resumed

Here is my view hierarchy:

  • CoordinatorLayout
    • AppBarLayout
    • ViewPager
      • 4 fragments

The first fragment has a WebView (the root is a WebView ).

When going between the pages really quick, I'd get this error eventually (Happens randomly, or on a certain spot that I can't figure out) swiping away from the fragment that has the WebView or towards it.

If I swipe between the fragments that don't contain WebViews, it works just fine no matter how fast or how many times I swipe.

Stacktrace:

java.lang.IndexOutOfBoundsException: getChildDrawingOrder() returned invalid index 1 (child count is 1)
    at android.view.ViewGroup.getAndVerifyPreorderedIndex(ViewGroup.java:2055)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4190)
    at android.view.View.draw(View.java:21956)
    at android.view.View.updateDisplayListIfDirty(View.java:20830)
    at android.view.View.draw(View.java:21685)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4432)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4193)
    at android.view.View.updateDisplayListIfDirty(View.java:20821)
    at android.view.View.draw(View.java:21685)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4432)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4193)
    at android.view.View.updateDisplayListIfDirty(View.java:20821)
    at android.view.View.draw(View.java:21685)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4432)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4217)
    at android.view.View.draw(View.java:21956)
    at androidx.viewpager.widget.ViewPager.draw(ViewPager.java:2426)
    at android.view.View.updateDisplayListIfDirty(View.java:20830)
    at android.view.View.draw(View.java:21685)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4432)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4193)
    at android.view.View.updateDisplayListIfDirty(View.java:20821)
    at android.view.View.draw(View.java:21685)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4432)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4193)
    at android.view.View.updateDisplayListIfDirty(View.java:20821)
    at android.view.View.draw(View.java:21685)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4432)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4193)
    at android.view.View.updateDisplayListIfDirty(View.java:20821)
    at android.view.View.draw(View.java:21685)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4432)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4193)
    at android.view.View.updateDisplayListIfDirty(View.java:20821)
    at android.view.View.draw(View.java:21685)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4432)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4193)
    at android.view.View.updateDisplayListIfDirty(View.java:20821)
    at android.view.View.draw(View.java:21685)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4432)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4193)
    at android.view.View.updateDisplayListIfDirty(View.java:20821)
    at android.view.View.draw(View.java:21685)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4432)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4193)
    at android.view.View.updateDisplayListIfDirty(View.java:20821)
    at android.view.View.draw(View.java:21685)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4432)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4193)
    at android.view.View.updateDisplayListIfDirty(View.java:20821)
    at android.view.View.draw(View.java:21685)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4432)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4193)
    at android.view.View.draw(View.java:21956)
    at com.android.internal.policy.DecorView.draw(DecorView.java:795)
    at android.view.View.updateDisplayListIfDirty(View.java:20830)
    at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:575)
    at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:581)
    at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:654)
    at android.view.ViewRootImpl.draw(ViewRootImpl.java:3761)
    at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:3489)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2777)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1745)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7730)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966)
    at android.view.Choreographer.doFrame(Choreographer.java:725)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:951)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7116)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:925)

It's difficult for me to understand why you are getting IndexOutOfBoundException without seeing some parts of your code. However, I can assume that, when the Fragment with the WebView is being loaded, it's trying to use some resource which is not available yet. Hence I would like to propose a workaround.

You might consider setting the setOffscreenPageLimit so that it does not reload the fragments even if you swipe down all the way. A sample code is the following.

ViewPager mViewpager = (ViewPager)findViewById(R.id.viewpager)
mViewPager.setOffscreenPageLimit(3);

I hope that solves your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM