Xamarin.Android中FindViewById获取Toolbar内TextView返回Null问题
哦,我一眼就看出问题所在了!你在MainPage.axml里放的是一个空的Toolbar,而包含toolbarMain_Name这个TextView的布局是单独的toolbarMain.xml——但你根本没把这个toolbar布局引入到MainPage的布局里啊!所以当你调用SetContentView(Resource.Layout.MainPage)后,当前布局里根本不存在toolbarMain_Name这个控件,自然会返回null引发空引用异常。
下面给你两种简单的解决思路,选哪种都能搞定问题:
解决思路一:用<include>标签复用toolbarMain.xml布局
把MainPage.axml里原来的空Toolbar替换成include标签,直接引入你写好的toolbarMain.xml布局:
修改后的MainPage.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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 引入独立的toolbar布局 --> <include layout="@layout/toolbarMain"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:id="@+id/imageView4" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:adjustViewBounds="true" android:src="@drawable/bitmap_homepage" /> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:id="@+id/ListSpaces" android:focusable="false" /> </LinearLayout> </LinearLayout>
这样修改后,MainPage布局就包含了带有TextView的Toolbar,你原来的Activity代码完全不需要改动,就能正常找到toolbarMain_Name控件了。
解决思路二:直接把TextView写到MainPage.axml的Toolbar里
如果你不想单独维护一个toolbar布局文件,也可以直接把TextView内嵌到MainPage的Toolbar中:
修改后的MainPage.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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:id="@+id/toolbarMain" android:minHeight="?attr/actionBarSize" android:background="?attr/colorAccent" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- 直接把TextView放到Toolbar内部 --> <TextView android:id="@+id/toolbarMain_Name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> </android.support.v7.widget.Toolbar> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:id="@+id/imageView4" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:adjustViewBounds="true" android:src="@drawable/bitmap_homepage" /> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:id="@+id/ListSpaces" android:focusable="false" /> </LinearLayout> </LinearLayout>
这种方式同样能解决问题,你的Activity代码保持不变即可。
最后提个小提醒:如果用第一种方案,两个布局里的Toolbar id都是toolbarMain,这里不会有冲突,但如果后续修改要注意;如果用第二种方案,原来的toolbarMain.xml就没用了,可以删掉避免冗余。
内容的提问来源于stack exchange,提问作者TBidaud
相关产品推荐
相关产品推荐

