java.lang.IllegalStateException: RecyclerView has no LayoutManager in Fragment

23,872

Solution 1

This question is quite old but for the benefit of people still facing this issue I thought I would leave an answer. I am just learning Android development and this issue was frustrating. Later on I found out that you are not supposed to add any views or view groups under RecyclerView. The layout that you want to appear under the RecyclerView needs to be defined separately in an independent resource file.

Then in the adapter implementation (the one that extends RecyclerView.Adapter<>, in the overridden method public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) when you inflate the layout for the view (this is inflating the layout for each item of the recycler view), you can specify the above separate layout that you created. Android then inflates this layout for every item that needs to be added to the recycler view.

This article helped me see this clearly.

Solution 2

RecyclerView is a ViewGroup that requires LayoutManager that will do the layouting of its children. In your case RecyclerView needs the child view to be passed to the LayoutManager, but I'm not sure if it is possible to pass xml reference of View to LayoutManager.

Solution 3

I had same issue when I tried to use android-parallax-recycleview.

The solution was simple, you can create the layout manager manager manually.

Code snippet from example-parallaxrecycler:

LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setOrientation(LinearLayoutManager.VERTICAL);
myRecycler.setLayoutManager(manager);

Solution 4

I ended up just inflating the views afterwards like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {      
    View rootView = inflater.inflate(R.layout.fragment_catalog_viewer, container, false);
    recyclerView = (RecyclerView)rootView.findViewById(R.id.my_recycler_view);
    getLayoutInflater(savedInstanceState).inflate(R.layout.catalog_child, container);        
    return rootView;
}
Share:
23,872
Edward van Raak
Author by

Edward van Raak

Senior Android Developer @ Nedap Healthcare

Updated on December 29, 2020

Comments

  • Edward van Raak
    Edward van Raak over 3 years

    I was in the process of changing an Activity into a Fragment and got the following error as soon as I inflated the RecyclerView.

    @Override
    public View onCreateView(LayoutInflater inflater, 
              ViewGroup container, Bundle savedInstanceState) {
    
        ----> View rootView = inflater.inflate(R.layout.layout_containing_recyclerview, 
                        container, false); 
    

    java.lang.IllegalStateException: RecyclerView has no LayoutManager

    Before I changed my Activity to a Fragment the inflate went just fine.

    Some further research showed that removing all my child elements from the recyclerview layout helped solve the problem. However I do not understand why that would change anything and why it did work work with an activity before.

    WORKS

    <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"   
        android:scrollbars="vertical" >    
    
    </android.support.v7.widget.RecyclerView>
    

    DOES NOT WORK

    <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"   
        android:scrollbars="vertical" >    
    
    
    <View
        android:id="@+id/randomview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
    
    </android.support.v7.widget.RecyclerView>
    

    Anything I am missing here?

  • yigit
    yigit over 9 years
    nope, it is not possible to pass items to RV directly. You have to use an adapter and a layout manager.
  • Mooing Duck
    Mooing Duck almost 9 years
    And where does myRecycler come from? At least in my code it comes from myRecycler = (RecyclerView) v.findViewById(R.id.recycler);, and v comes from inflater.inflate(R.layout.my_recycler_view, container, false), which is the line with the error. How does placing code after the exception prevent the exception from occuring?
  • Walid Ammar
    Walid Ammar almost 9 years
    mRecyclerView = (RecyclerView) findViewById(R.id.main_recycler_view);
  • slinden77
    slinden77 almost 8 years
    What @MohammadWalid probably means is that EVERY ReclyclerView needs a LayoutManager. Even though it might appear that your app crashes while inflating a layout that actually does have a LayoutManager, it's not unprobable that it crashes on a different RecylerView. I am seeing this behaviour and it costed me a few hours :-) This answer seems incomplete though.
  • Siavash
    Siavash over 6 years
    this was my issue, I had accidentally put a view inside of my recycler view. thanks