I'm trying to put in a toolbar created by myself a centrered title. It was working exactly as intended with a basic toolbar but for different purpose I had to switch to Android.Support.V7 and now it isn't working anymore due to a FindViewById returning null now
Here's my code: (working on Xamarin.Android)
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" /> <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> toolbarMain.xml
<?xml version="1.0" encoding="utf-8" ?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbarMain" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?attr/actionBarSize" android:background="?attr/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <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> MainPageActivity.cs
public static string TitleFolder = "Welcome"; protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.MainPage); //Put toolbar var toolbar = FindViewById<Toolbar>(Resource.Id.toolbarMain); var ToolTitle = FindViewById<TextView>(Resource.Id.toolbarMain_Name); SetSupportActionBar(toolbar); SupportActionBar.SetDisplayShowTitleEnabled(false); ToolTitle.Text = TitleFolder; } The error Visual Studio is giving me is
System.NullReferenceException: Object reference not set to an instance of an object.
When looking at the variables, the program is finding all the ids in Ressource.Designer, but return null to Tooltitlebut toolbar is absolutely okay.
Any ideas ? It's slowly driving me crazy since it was working perfectly fine on a standard toolbar... Thanks a lot