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

后端 未结 5 1617
庸人自扰
庸人自扰 2020-12-30 06: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 onCrea         


        
相关标签:
5条回答
  • 2020-12-30 06:57

    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);
    
    0 讨论(0)
  • 2020-12-30 07:03

    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.

    0 讨论(0)
  • 2020-12-30 07:03

    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.

    0 讨论(0)
  • 2020-12-30 07:09

    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;
            }
    
    0 讨论(0)
  • 2020-12-30 07:17

    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;
    }
    
    0 讨论(0)
提交回复
热议问题