2016-05-31 29 views
0

我收到以下错误DrawerLayout而其中有android.support.design.widget.NavigationView为drawerlayout NULL异常时xamarin安卓

System.NullReferenceException相同的活动增加RecyclerView加入recyclerview未设置为一个对象

这里的一个实例是我的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);