weixin_39610722
weixin_39610722
2020-12-08 23:08

NPE onMeasure

Already updated to v1.1.0 from 1.0.13 and still have the same issue:


java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.view.PagerAdapter android.support.v4.view.ViewPager.getAdapter()' on a null object reference
        at com.shuhart.bubblepagerindicator.BubblePageIndicator.getCount(BubblePageIndicator.java:185)
        at com.shuhart.bubblepagerindicator.BubblePageIndicator.getInitialStartX(BubblePageIndicator.java:480)
        at com.shuhart.bubblepagerindicator.BubblePageIndicator.measureStartX(BubblePageIndicator.java:473)
        at com.shuhart.bubblepagerindicator.BubblePageIndicator.onMeasure(BubblePageIndicator.java:468)
        at android.view.View.measure(View.java:22002)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
        at android.view.View.measure(View.java:22002)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.support.design.widget.CollapsingToolbarLayout.onMeasure(CollapsingToolbarLayout.java:411)
        at android.view.View.measure(View.java:22002)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
        at android.support.design.widget.AppBarLayout.onMeasure(AppBarLayout.java:234)
        at android.view.View.measure(View.java:22002)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
        at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:739)
        at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:811)
        at android.view.View.measure(View.java:22002)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:141)
        at android.view.View.measure(View.java:22002)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:22002)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:22002)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
        at android.view.View.measure(View.java:22002)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:721)
        at android.view.View.measure(View.java:22002)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2410)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1498)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1751)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6733)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
        at android.view.Choreographer.doCallbacks(Choreographer.java:723)
        at android.view.Choreographer.doFrame(Choreographer.java:658)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

I defined it in XML like this:

xml
<com.shuhart.bubblepagerindicator.bubblepageindicator android:id="@+id/bubble_indicator" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:layout_marginbottom="10dp" android:fitssystemwindows="true" android:visibility="visible" app:bpi_fillcolor="/red" app:bpi_pagecolor="/green" app:bpi_radius="8dp" app:bpi_marginbetweencircles="6dp" app:bpi_onsurfacecount="3" app:bpi_risingcount="1"></com.shuhart.bubblepagerindicator.bubblepageindicator>

Looks like onMeasure is being called as soon as the activity is created but BubblePageIndicator doesn't have any viewPager reference yet because I set it later in the lifecycle, and there's no null checks in the BubblePageIndicator class:

java

protected int getCount() {
    return viewPager.getAdapter().getCount();
}

该提问来源于开源项目:shuhart/BubblePagerIndicator

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

4条回答

为你推荐

换一换