2

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

1
  • 1
    Thanks for the answer, I followed the one from Cody W. since I'll be using the toolbar for different Activities so it'll be cleaner for me. But your solution seemed a good way to go Commented May 17, 2018 at 14:31

2 Answers 2

1

V7 support toolbar has different setup, call :

setSupportActionBar(findViewById(R.id.toolbarMain)) 

Go through https://developer.android.com/training/appbar/setting-up for details.

Sign up to request clarification or add additional context in comments.

1 Comment

Wasn't exactly what I intended to resolve and your answer didn't seem to work on my side but thanks anyway
0

You are setting the contentview to MainPage.xml, but toolbarMain_Name is not an element of that layout. You will need to either add the textview to your MainPage.xml or replace your declaration of the Toolbar with an include statement.

<include layout="@layout/your_toolbar_layout_name"/> 

1 Comment

It worked, thanks a lot ! I had the include tag on the first toolbar but I erased it while creating it again with AppCompat. I was following the official Xamarin doc but I guess I did it wrong while changing the code

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.