2016-02-17 72 views
0

我在我的活動onCreate功能如下代碼:無法將LAYOUTManager設置爲RecyclerView。顯示空指針的RecyclerView

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    LinearLayoutManager manager = new LinearLayoutManager(this); 
    manager.setOrientation(LinearLayoutManager.VERTICAL); 
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 
    recyclerView.setLayoutManager(manager); 
    setContentView(R.layout.my_layout); 
    // some more code 

my_layout.xml如下所示:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/annonce.main.coordinator" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:ignore="RtlHardcoded"> 

    <android.support.design.widget.AppBarLayout...> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recycler_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scrollbars="none" 
     app:behavior_overlapTop="78dp" 
     android:fillViewport="true" 
     android:layout_gravity="fill_vertical" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <android.support.v7.widget.CardView 
      xmlns:card_view="http://schemas.android.com/apk/res-auto" 
      android:id="@+id/card_view" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="8dp" 
      android:layout_marginRight="8dp" 
      android:layout_marginBottom="8dp" 
      card_view:cardCornerRadius="4dp"> 

      <ListView 
       android:id="@+id/listView" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:choiceMode="singleChoice" 
       android:scrollbars="vertical" 
       app:behavior_overlapTop="78dp" 
       app:layout_behavior="@string/appbar_scrolling_view_behavior"> 
      </ListView> 

     </android.support.v7.widget.CardView> 
    </android.support.v7.widget.RecyclerView> 
</android.support.design.widget.CoordinatorLayout> 

我收到錯誤:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method void 
android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager) on a null object reference` in the line recyclerView.setLayoutManager(manager); 

我確實嘗試過在以前的sof文章中的類似查詢,但沒有一個看起來像d解決我的問題很多。你能告訴我我的代碼在哪裏出錯嗎?謝謝。

+1

移動'的setContentView(R.layout.my_layout);'到後'super.onCreate(savedInstanceState)頂部;' –

+0

即引發錯誤'所致:java.lang.IllegalStateException:RecyclerView沒有LayoutManager'這是非常理由我在之前添加了'LinearLayoutManager'代碼。 –

+0

對不起,我剛剛注意到。你的佈局是錯誤的。 RecyclerView只能包含由** RecylerViewAdapter **填充的視圖。檢查這個鏈接學習如何使用RecyclerView http://code.tutsplus.com/tutorials/getting-started-with-recyclerview-and-cardview-on-android--cms-23465 –

回答

0

1)在 「@Much溢出」 答覆中提到一樣,你onCreate()方法更改爲:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.my_layout); // setting the layout first 

    LinearLayoutManager manager = new LinearLayoutManager(this); 
    manager.setOrientation(LinearLayoutManager.VERTICAL); 
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 
    recyclerView.setLayoutManager(manager); 
} 

2)RecyclerView XML刪除多餘的孩子的。

my_layout.xml應該看起來像:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/annonce.main.coordinator" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:ignore="RtlHardcoded"> 

    <android.support.design.widget.AppBarLayout...> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recycler_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scrollbars="none" 
     app:behavior_overlapTop="78dp" 
     android:fillViewport="true" 
     android:layout_gravity="fill_vertical" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    </android.support.v7.widget.RecyclerView> 
</android.support.design.widget.CoordinatorLayout> 

如果你想在你的RecyclerView添加視圖,你應該使用Adapter

0

在設置佈局之前,您正在嘗試查找RecyclerView。這樣做

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.my_layout); // setting the layout first 

    LinearLayoutManager manager = new LinearLayoutManager(this); 
    manager.setOrientation(LinearLayoutManager.VERTICAL); 
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 
    recyclerView.setLayoutManager(manager); 
} 
+0

這樣做會拋出錯誤'引起通過:java.lang.IllegalStateException:RecyclerView沒有佈線管理器'行setContentView(R.layout.my_layout);' –

+0

對不起,我剛剛注意到。你的佈局是錯誤的。 RecyclerView只能包含由** RecylerViewAdapter **填充的視圖。檢查此鏈接瞭解如何使用RecyclerView http://code.tutsplus.com/tutorials/getting-started-with-recyclerview-and-cardview-on-android--cms-23465 –

0

爲什麼在RecyclerView裏面有CardView和listView?我從來沒有見過這樣的佈局。你的佈局有問題。

嘗試從佈局文件中刪除recyclerView的子項。

+0

因此,我需要刪除CardView從那裏到其他一些xml文件,然後在RecyclerView中刪除那個新的xml文件? –

+0

RecyclerView使用一個適配器,充當回收站視圖中顯示的數據和行之間的橋樑。適配器與封裝視圖的ViewHolder相關聯。如果你看一個簡單的示例演示RecyclerView,它會很清楚。 [鏈接](http://developer.android.com/samples/RecyclerView/index.html) – greenrobo