Setting scrollbars property in RecyclerView crashes my application

I recently started using RecyclerView to replace the old ListView. But whenever I use an element android:scrollbars="vertical"in android.support.v7.widget.RecyclerView, my application 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 use 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 the my_fragment.xmlfile:

<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 fine if I do not use android:scrollbars="vertical". Thank you for your help.

+4
source share
1 answer

When using, RecyclerViewyou do not need to specify a mapping scrollbars; this is a task LayoutManagerwhere you indicate whether it is a vertical or horizontal orientation, and, of course, the scroll bar display automatically changes any orientation you choose.

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

//for horizontal scrollbar and orientation 
LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false); 
0

All Articles