【问题标题】:java.lang.IllegalStateException: RecyclerView has no LayoutManager in Fragmentjava.lang.IllegalStateException:RecyclerView 在 Fragment 中没有 LayoutManager
【发布时间】:2015-02-18 06:14:18
【问题描述】:

我正在将 Activity 更改为 Fragment 并在我为 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 没有 LayoutManager

在我将 Activity 更改为 Fragment 之前,膨胀效果很好。

一些进一步的研究表明,从 recyclerview 布局中删除我的所有子元素有助于解决问题。但是,我不明白为什么这会改变任何事情以及为什么它之前确实可以与活动一起使用。

有效

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

不工作

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

我在这里缺少什么?

【问题讨论】:

  • 删除视图是可行的,但这并不是解决整个问题的方法,因为我们有时确实需要放置在那里的视图

标签: android android-fragments android-5.0-lollipop android-recyclerview


【解决方案1】:

这个问题已经很老了,但为了让仍然面临这个问题的人受益,我想我会留下一个答案。我只是在学习 Android 开发,这个问题令人沮丧。后来我发现你不应该在RecyclerView 下添加任何视图或视图组。您希望在RecyclerView 下出现的布局需要在独立的资源文件中单独定义。

然后在适配器实现中(扩展RecyclerView.Adapter&lt;&gt;,在覆盖方法public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)当你为视图膨胀布局时(这是为回收器视图的每个项目膨胀布局),你可以指定您创建的上述单独布局。Android 然后为需要添加到回收器视图的每个项目扩展此布局。

This article 帮助我清楚地看到了这一点。

【讨论】:

    【解决方案2】:

    RecyclerView 是一个 ViewGroup ,它需要 LayoutManager 来对其子项进行布局。在您的情况下,RecyclerView 需要将子视图传递给 LayoutManager,但我不确定是否可以将视图的 xml 引用传递给 LayoutManager

    【讨论】:

    • 不,不能直接将项目传递给 RV。您必须使用适配器和布局管理器。
    • 这是我的问题,我不小心在回收站视图中放置了一个视图。谢谢
    【解决方案3】:

    我在尝试使用 android-parallax-recycleview 时遇到了同样的问题。

    解决方案很简单,您可以手动创建布局管理器。

    来自example-parallaxrecycler的代码sn-p:

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

    【讨论】:

    • 那么myRecycler 来自哪里?至少在我的代码中它来自myRecycler = (RecyclerView) v.findViewById(R.id.recycler);,而v 来自inflater.inflate(R.layout.my_recycler_view, container, false),这是错误所在的行。异常后加代码如何防止异常发生?
    • mRecyclerView = (RecyclerView) findViewById(R.id.main_recycler_view);
    • @MohammadWalid 可能意味着每个ReclyclerView 都需要LayoutManager。即使看起来你的应用程序在扩展实际上确实有LayoutManager 的布局时会崩溃,但它在不同的RecylerView 上崩溃也不是不可能的。我看到了这种行为,花了我几个小时 :-) 不过这个答案似乎不完整。
    【解决方案4】:

    我最终只是像这样夸大视图:

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

    【讨论】:

      【解决方案5】:

      即使我在onCreateView中初始化它仍然是空指针异常。

      以编程方式添加。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-10
        相关资源
        最近更新 更多