7

我正在將Activity更改爲Fragment,並在我充氣RecyclerView後立即得到以下錯誤。java.lang.IllegalStateException:RecyclerView在片段中沒有LayoutManager

@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沒有佈局管理

之前,我改變了我的活動,以片段的充氣去就好了。

一些進一步的研究表明,從recyclerview佈局中刪除所有我的孩子元素有助於解決問題。然而我不明白爲什麼這會改變什麼,以及爲什麼它的工作與之前的一項活動有關。

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> 

不起作用

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

什麼我是缺少在這裏?

+0

刪除視圖的工作,但它不是解決整個問題,因爲我們有時真的需要我們放在那裏 – larrytech 2015-09-11 01:39:08

回答

1

RecyclerViewViewGroup,它需要LayoutManager,將做其子項的佈局。在你的情況RecyclerView需要將子視圖傳遞給LayoutManager,但我不確定是否有可能將視圖的xml引用傳遞給LayoutManager

+0

nope的意見,它不可能直接傳遞項目RV。您必須使用適配器和佈局管理器。 – yigit 2014-12-21 03:07:33

+0

這是我的問題,我不小心把我的回收者視圖的內部視圖。謝謝 – Siavash 2017-08-25 17:02:29

1

我結束了剛剛膨脹的意見之後這樣的:

@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

還有空指針異常,即使我初始化它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; 
     } 
2

當我試圖使用android-parallax-recycleview時,我遇到了同樣的問題。

該解決方案很簡單,您可以手動創建佈局管理器管理器。從example-parallaxrecycler

代碼片段:

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

'myRecycler'從哪裏來?至少在我的代碼中,它來自'myRecycler =(RecyclerView)v.findViewById(R.id.recycler);''和'v'來自'inflater.inflate(R.layout.my_recycler_view,container,false)',這是錯誤的路線。在異常之後放置代碼如何防止發生異常? – 2015-08-18 21:45:27

+0

'mRecyclerView =(RecyclerView)findViewById(R.id.main_recycler_view);' – 2015-08-19 04:30:06

+0

@MohammadWalid可能意味着每一個'ReclyclerView'都需要一個'LayoutManager'。即使它看起來應用程序崩潰時,實際上確實有一個「LayoutManager」的佈局,但它不會在不同的'RecylerView'上崩潰。我看到這種行爲,並花費了我幾個小時:-)這個答案似乎不完整。 – slinden77 2016-07-23 16:42:44

26

這個問題是很老,但對於人仍然面臨這個問題的好處,我想我會離開一個答案。我剛剛學習Android開發,這個問題令人沮喪。後來我發現你不應該在RecyclerView之下添加任何視圖或查看組。您想在RecyclerView下顯示的佈局需要在獨立資源文件中單獨定義。

然後在適配器實現(即延伸RecyclerView.Adapter<>,在重寫的方法public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)時膨脹視圖的佈局(這是充氣的再循環器視圖的每個項目的佈局)的一個,可以指定上述分開您創建的佈局。然後,Android爲每個需要添加到回收站視圖的項目擴充此佈局。

This article幫我看清楚了。