2016-05-31 10 views
0

Ich erhalte für DrawerLayout folgende Fehler während RecyclerView in der gleichen Aktivität Zugabe, die android.support.design.widget.NavigationViewNULL Ausnahme für drawerlayout beim Hinzufügen recyclerview in Xamarin android

System.NullReferenceException hat: Der Objektverweis nicht auf eine Instanz eines Objekts

Hier ist mein C# -Code

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

Hier meine axml Datei Code

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

Wenn ich folgenden Code aus axml entfernen wird, dann wird es funktionieren. Ich weiß nicht, warum DrawerLayout NULL wird, wenn ich android.support.v7.widget.RecyclerView in meiner XML-Datei hinzufüge?

<android.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scrollbars="vertical" /> 

Antwort

0

Sie müssen diese Einstellung vornehmen zuerst:

SetContentView(Resource.Layout.LayoutContainingDrawer); 

bevor Sie versuchen, es zu finden:

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