Nullpointerexception on dispatchDraw with ViewPager in nested fragment with PageTransformer

前端 未结 1 1888
被撕碎了的回忆
被撕碎了的回忆 2021-02-09 19:35

I\'m having a pretty nondescript bug in the Android app I\'m working on. I have a fragment, which in turns contains a ViewPager that\'s backed by a FragmentStatePagerAdapter. Th

1条回答
  •  别跟我提以往
    2021-02-09 19:54

    I don't have enough rep to comment, so I'll answer here.

    I ran into exactly the same issue. Looking at the source for ViewGroup, it was running through the children of the android.support.v4.view.ViewPager and hitting the NPE there. As you stated, removing the page transformer worked, as did removing the PagerTabStrip for me. I'm looking at it right now, but it looks like some kind of incompatibility between the page transformer and the PagerTabStrip.

    Specifically, when the ViewPager is asked to redraw itself, at the top of your stack trace:

    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2946)
    at android.view.View.draw(View.java:14476)
    at android.support.v4.view.ViewPager.draw(ViewPager.java:2171)
    

    For me, at this point the ViewPager has only a single child - the PagerTabStrip. In dispatchDraw in ViewGroup, this is where the NPE pops up:

    for (int i = 0; i < count; i++) {
        final View child = children[getChildDrawingOrder(count, i)];
        if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
            more |= drawChild(canvas, child, drawingTime);
        }
    }
    

    Where children[] contains the PagerTabStrip as it's 0th entry, for some reason, children[getChildDrawingOrder(count, i)] is evaluating to null.

    getchildDrawingOrder(count, i) 
    

    is not overloaded in my implementation, so it just returns i. Its beyond me as to why this is evaluating to null - it certainly seems like it should return the PagerTabStrip, but it's not and it's generating the NPE when trying to access the children[0]'s members.

    I have no solution for you, but hopefully this is similar to what you've run into. Let me know if you find out anything.

    EDIT

    Not quite sure why, but replacing my ViewPager with this seems to have resolved the crash:

    package ca.test;
    
    import android.content.Context;
    import android.support.v4.view.ViewPager;
    import android.util.AttributeSet;
    
    public class TestViewPager extends ViewPager {
    
        public TestViewPager(Context context) {
            super(context);
        }
    
        public TestViewPager(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        protected int getChildDrawingOrder(int childCount, int i) {
            return i;
        }
    
    }
    

    0 讨论(0)
提交回复
热议问题