4

I recently started using RecyclerView replacing older ListView. But whenever I use android:scrollbars="vertical" in android.support.v7.widget.RecyclerView element, my app crashes with the following error: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollVertically()' on a null object reference

This is how I'm using RecyclerView:

View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.my_fragment, container, false);
    return rootView;
}
protected void onPostExecute(Boolean result) {
    RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.recyclerview);
    LinearLayoutManager llm = new LinearLayoutManager(context);
    rv.setLayoutManager(llm);
    RecycleViewAdapter adapter = new RecycleViewAdapter(myRecyclerViewAdapter);
    rv.setHasFixedSize(true);
    rv.setAdapter(adapter);
}

And this is my_fragment.xml file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

    </android.support.v7.widget.RecyclerView>

</LinearLayout>

Everything works perfectly fine if I don't use android:scrollbars="vertical". Thanks for your help.

Basit
  • 1,830
  • 2
  • 31
  • 50
  • check this link http://stackoverflow.com/questions/27056379/is-there-any-way-to-enable-scrollbars-for-recyclerview-in-code – Jignesh Jain May 04 '15 at 05:11
  • @JigneshJain I did try adding `android:scrollbars="vertical"` and it crashes my app, as mentioned in the question. – Basit May 04 '15 at 05:18
  • check this https://code.google.com/p/android/issues/detail?id=78545 – Jignesh Jain May 04 '15 at 05:23
  • @JigneshJain According to the forum, the problem was with older versions of support libraries. I'm using 22.0 with my project. Let me try changing it to 21.0.2 to see if it solves the problem. – Basit May 04 '15 at 05:30
  • @JigneshJain Nop. Did that. Still crashes. – Basit May 04 '15 at 05:36
  • Can you create a bug report on b.android.com with full stack trace? I thought i've guarded all places but maybe something is missing. Also, an easy workaround would be setting a layout manager when onCreateView is called. – yigit May 05 '15 at 04:17
  • @yigit Let me look into b.android.com Could you elaborate setting a layout manager in `onCreateView` part. I would have to initialize RecyclerView at the same time, yes? – Basit May 05 '15 at 04:46
  • Assuming recyclerview is inside my_fragment, it is already initialized when you inflate it. – yigit May 05 '15 at 05:44
  • Not technically. FrameLayout is defined in a separate file and RecyclerView in an another. An activity is inflating fragments. – Basit May 05 '15 at 06:00

1 Answers1

0

When using the RecyclerView you don't really need to specify the scrollbars display; it is the LayoutManager job where you specify if it is Vertical or Horizontal orientation and of course the scrollbar display will automatically change whatever orientation you choose.

//for vertical scrollbar and orientation
LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);  

//for horizontal scrollbar and orientation 
LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false); 
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63