2016-05-31 7 views
0

をxamarinでrecyclerviewを追加するときandroid.support.design.widget.NavigationViewNULL例外アンドロイド

System.NullReferenceException持って同じ活動にRecyclerViewを追加している間、私はDrawerLayoutため、次のエラーを取得しています:オブジェクト参照をオブジェクトここ

のインスタンスに設定されていない私のC#のコードは

DrawerLayout drawerLayout; 
protected override void OnCreate(Bundle savedInstanceState) 
{ 
     base.OnCreate(savedInstanceState); 
     drawerLayout = this.FindViewById<DrawerLayout>(Resource.Id.drawer_layout); 
     drawerLayout.CloseDrawers(); 
} 

であるここに私のAXMLファイルがありますコード

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:id="@+id/toolbar_layout"> 
    <include 
     android:id="@+id/toolbar" 
     layout="@layout/toolbar" 
     app:layout_scrollFlags="scroll|enterAlways" /> 
    </android.support.design.widget.AppBarLayout> 

    </RelativeLayout> 
<android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:headerLayout="@layout/nav_header" 
     app:menu="@menu/nav_menu" /> 

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:background="#e7e6e6" 
      android:paddingTop="60dp"> 
    <android.support.v7.widget.RecyclerView 

     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scrollbars="vertical" /> 
</LinearLayout> 

私はそれが正常に動作しますAXMLから、次のコードを削除する場合は。なぜ私はxmlファイルにandroid.support.v7.widget.RecyclerViewを追加するときにDrawerLayoutがNULLになるのか分かりませんか?

あなたが最初にこれを設定する必要が
<android.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scrollbars="vertical" /> 

答えて

0

SetContentView(Resource.Layout.LayoutContainingDrawer); 

あなたはそれを見つけることを試みる前に:

drawerLayout = this.FindViewById<DrawerLayout>(Resource.Id.drawer_layout);