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

后端 未结 5 1629
庸人自扰
庸人自扰 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 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.

提交回复
热议问题