danoz73 / RecyclerViewFastScroller

A Fast Scroller for the RecyclerView world!
Other
1.13k stars 211 forks source link

Null Pointer Exception #41

Open crazybee0909 opened 9 years ago

crazybee0909 commented 9 years ago

This might be a small issue but I'm not able to get past this. My fragment gives a null pointer exception like- Attempt to invoke virtual method 'android.support.v7.widget.RecyclerView$LayoutManager android.support.v7.widget.RecyclerView.getLayoutManager()' on a null object reference

This is my onCreateView of the fragment -

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.list, container, false);
    RecyclerView songRecyclerView = (RecyclerView) view.findViewById(R.id.list);
    songRecyclerView.addItemDecoration(new          BackgroundDecoration(Themes.getBackgroundElevated()));
    songRecyclerView.addItemDecoration(new DividerDecoration(getActivity()));
     int paddingH =(int) getActivity().getResources().getDimension(R.dimen.global_padding);
    view.setPadding(paddingH, 0, paddingH, 0);
    songRecyclerView.setAdapter(new Adapter());
    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    songRecyclerView.setLayoutManager(layoutManager);
    VerticalRecyclerViewFastScroller fastScroller = (VerticalRecyclerViewFastScroller)  view.findViewById(R.id.fast_scroller);
    fastScroller.setRecyclerView(songRecyclerView);
    songRecyclerView.addOnScrollListener(fastScroller.getOnScrollListener());

    return view;
}

I can't figure out what is going wrong. Any ideas how to fix this minor issue?

0xKiwi commented 9 years ago

I had this issue but then realized I wasn't using the fastscroller on all recyclerview fragments that had it in the list.

So the fix (for me at least): Make sure you use the fastscroller in all fragments that use the layout with it. If you'd like to only use the fast scroller in only one of the fragments but not another then make another layout for it just with the fast scroller addded.

mrArtCore commented 8 years ago

I got the same exception. I used fastScroller only in 1 layout, so "fix tip" of @ThatKawaiiGuy doesn`t work for me

XML of my Activity :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    >

  <include layout="@layout/view_toolbar_main"
      android:id="@+id/tbToolbar" />

  <android.support.v7.widget.RecyclerView
      android:id="@+id/lvContactsList"
      android:layout_below="@+id/tbToolbar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_above="@+id/vBottomSection"
      />

  <xyz.danoz.recyclerviewfastscroller.vertical.VerticalRecyclerViewFastScroller
      android:id="@+id/fast_scroller"
      android:layout_width="@dimen/general_mid"
      android:layout_height="wrap_content"
      android:layout_alignParentEnd="true"
      android:layout_alignTop="@+id/lvContactsList"
      android:layout_alignBottom="@+id/lvContactsList"
      />

  <ProgressBar
      android:id="@+id/pbProgressBar"
      android:layout_width="@dimen/general_xx_large"
      android:layout_height="@dimen/general_xx_large"
      android:visibility="visible"
      android:layout_centerInParent="true"
      />

  <include layout="@layout/view_bottom_sheet_header"
      android:id="@+id/vBottomSection"
      android:layout_height="@dimen/general_x_large"
      android:layout_width="wrap_content"
      android:layout_alignParentBottom="true"
      />
</RelativeLayout>

in my Activity OnCreateMethod:

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)

  contactsAdapter = /* code of adapter creation */
  lvContactsList = findViewById(R.id.lvContactsList) as RecyclerView

  lvContactsList.adapter = contactsAdapter
  val lManager = LinearLayoutManager(this@KnownContactsActivity)
  lManager.orientation = LinearLayoutManager.VERTICAL
  lvContactsList.layoutManager = lManager

  lvContactsList.apply { 
    adapter = contactsAdapter
  }
  fastScroller = findViewById(R.id.fast_scroller) as VerticalRecyclerViewFastScroller
  fastScroller.setRecyclerView(lvContactsList)
  lvContactsList.setOnScrollListener(fastScroller.onScrollListener)

  progressBar.hide()

}

If I remove FastScroller -> all code works fine without crashing...

liya7907 commented 7 years ago

@ThatKawaiiGuy your advise is workable,but i think it is still a bug of this libary,not every fragment need to decorate for the recyclerview inside.