FutureMind / recycler-fast-scroll

Provides fast scroll and section idexer for recycler view
Apache License 2.0
492 stars 105 forks source link

Null pointer exception in design view #51

Open karunanaik opened 7 years ago

karunanaik commented 7 years ago

java.lang.NullPointerException   at com.futuremind.recyclerviewfastscroll.RecyclerViewScrollListener.updateHandlePosition(RecyclerViewScrollListener.java:47)   at com.futuremind.recyclerviewfastscroll.FastScroller.onLayout_Original(FastScroller.java:168)   at com.futuremind.recyclerviewfastscroll.FastScroller.onLayout(FastScroller.java:-1)   at android.view.View.layout(View.java:17520)   at android.view.ViewGroup.layout(ViewGroup.java:5612)   at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)   at android.view.View.layout(View.java:17520)   at android.view.ViewGroup.layout(ViewGroup.java:5612)   at android.support.constraint.ConstraintLayout.onLayout_Original(ConstraintLayout.java:1127)   at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:-1)   at android.view.View.layout(View.java:17520)   at android.view.ViewGroup.layout(ViewGroup.java:5612)   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)   at android.widget.FrameLayout.onLayout(FrameLayout.java:261)   at android.view.View.layout(View.java:17520)   at android.view.ViewGroup.layout(ViewGroup.java:5612)   at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)   at android.view.View.layout(View.java:17520)   at android.view.ViewGroup.layout(ViewGroup.java:5612)

I get the above null pointer exception in my design view.

Below is my xml:

` <RelativeLayout android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/callScheduleType">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/callSchedules"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <com.futuremind.recyclerviewfastscroll.FastScroller
        android:id="@+id/fastScroller"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentEnd="true"
        android:orientation="vertical"
        app:fastscroll__bubbleColor="@color/colorGold"
        app:fastscroll__bubbleTextAppearance="@style/StyledScrollerTextAppearance"/>

</RelativeLayout>`

Can you please help me and let me know what am I missing here?

ozv101 commented 7 years ago

@karunanaik It's some bug with onLayout. It thinks the RecyclerView has been set. Found a work around: create your own CustomFastScroller extends FastScroller, then override onLayout:

 @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (recyclerView != null) {
            super.onLayout(changed, l, t, r, b);
        }
    }

Also,

@Override
    public void setRecyclerView(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
        super.setRecyclerView(recyclerView);

        requestLayout();
    }

then call setRecyclerView on your CustomFastScroller.

kalyanimvn commented 6 years ago

I got same error.But i fixed by using above code.The below code working fine.Thanks @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { if (recyclerView != null) { super.onLayout(changed, l, t, r, b); } } Also,

@Override public void setRecyclerView(RecyclerView recyclerView) { this.recyclerView = recyclerView; super.setRecyclerView(recyclerView);

    requestLayout();
}