java.lang.IllegalStateException: RecyclerView has

2019-02-06 17:50发布

问题:

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?

回答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.



回答2:

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);


回答3:

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;
}


回答4:

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.



回答5:

Still null pointer exception even if I initialize it in onCreateView.

Add it programatically.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

</LinearLayout>


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

...
    // create recyclerview
...
            LinearLayout llContainer = (LinearLayout)  v.findViewById(R.id.llContainer);
            llContainer.addView(recyclerView);

            return v;
        }