1

Hi I'm new to Xamarin and trying to find a view in OnCreate() in MainActivity but it can't be found(?) and it's returned as null.

protected override void OnCreate(Bundle bundle)
    {

        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(bundle);
        Xamarin.Forms.Forms.Init(this, bundle);

        LoadApplication(new App(new AndroidInitializer()));



        Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
    }

Toolbar :

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

I've downloaded others projects with the same code that works fine. What am I missing? (https://github.com/UdaraAlwis/Xamarin-Playground/tree/master/XFNavBarBackBtnClickOverride)

1 Answers1

1

You forgot to this.SetContentView(Resource.Layout.Main); inside your OnCreate() check it

protected override void OnCreate(Bundle bundle)
    {

        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);// add here your layout file
        Xamarin.Forms.Forms.Init(this, bundle);

        LoadApplication(new App(new AndroidInitializer()));



        Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
    }
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • 1
    Added to Toolbar.axml file instead of Main (since I didn't know what Main does). But it seemed to fix my problem. Thanks you – Mattias Greger May 08 '18 at 10:31
  • What does that SetContentView do? Because I'm getting 'Java.Lang.RuntimeException: Unable to resume activity' error when trying to set the toolbar to SetSupportActionBar(toolbar). Any ideas? – Mattias Greger May 08 '18 at 10:47
  • @MattiasGreger check this https://stackoverflow.com/questions/24706348/what-is-setcontentviewr-layout-main – AskNilesh May 08 '18 at 10:48
  • Ok. To new to understand that. I dont have a Main. Im using Xamarin Forms. And this is a new project from their template. Doesn't seem to use that Resource.Layout.Main – Mattias Greger May 08 '18 at 10:51
  • @MattiasGreger here `Resource.Layout.Main` means your layout file – AskNilesh May 08 '18 at 10:55
  • Ok. That I somewhat understand but since Im using Xamarin Forms. There is no Layout file in Resource -> Layout. There is a shared project for XML files. – Mattias Greger May 08 '18 at 11:05
  • @MattiasGreger check this sample demo https://github.com/xamarin/monodroid-samples you will get idea – AskNilesh May 08 '18 at 11:07
  • 1
    Will check it out. Thanks for your time – Mattias Greger May 08 '18 at 11:11