locked
SetActionBar(toolbar) doesn't work in visual studio RRS feed

  • Question

  • User240063 posted

    hi, i get a Java.Lang.NoSuchMethodError: no method with name='setActionBar' signature='(Landroid/widget/Toolbar;)V' in class Landroid/app/Activity; error when my debugger hit the function

    SetActionBar(toolbar)

    in visual studio 2015. pls can someone guide me on how to go about this the right way?

    here's my main activity code

    `protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle);

            var dashtoolbar = FindViewById<Toolbar>(Resource.Id.dashtoolbar);
            SetActionBar(dashtoolbar);
            ActionBar.Title = "DashBoard";
            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);
        }`
    

    here's my layout page <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/relativeLayout1"> <include android:id="@+id/dashtoolbar" layout="@layout/dashtoolbar" /> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:minWidth="25px" android:minHeight="25px"> <Button android:text="My Button" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button1" /> </LinearLayout> </RelativeLayout>

    here's my toolbar xml code <Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dashtoolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?android:attr/actionBarSize" android:background="?android:attr/colorPrimary" android:theme="@android:style/ThemeOverlay.Material.Dark.ActionBar"/>

    Tuesday, September 27, 2016 7:36 AM

Answers

  • User6666 posted

    First, you should check that dashboard is not null... before using it as arguments.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, September 27, 2016 4:56 PM
  • User208615 posted

    Have you noticed that two objects have identical ID?

    The include has an id of dashtoolbar

    <include
        android:id="@+id/dashtoolbar"
        layout="@layout/dashtoolbar" />
    

    and the actual toolbar has the same id: dashtoolbar

    <Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/dashtoolbar"
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, September 29, 2016 12:35 PM

All replies

  • User6666 posted

    First, you should check that dashboard is not null... before using it as arguments.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, September 27, 2016 4:56 PM
  • User208615 posted

    how about you SetContentView (Resource.Layout.Main); BEFORE you start referencing widgets :)

    Wednesday, September 28, 2016 3:26 PM
  • User240063 posted

    @nicolasETIENNE , yes dashtoolbar equals null, y is that? the resource designer has registered it.

    @Michael_Shapiro i have edited my code to give me this, yet i still get the error

    `base.OnCreate(bundle);

            SetContentView (Resource.Layout.Main);
            var dashtoolbar = FindViewById<Toolbar>(Resource.Id.dashtoolbar);
            SetActionBar(dashtoolbar);
            ActionBar.Title = "DashBoard";` 
    
    Thursday, September 29, 2016 6:01 AM
  • User208615 posted

    Have you noticed that two objects have identical ID?

    The include has an id of dashtoolbar

    <include
        android:id="@+id/dashtoolbar"
        layout="@layout/dashtoolbar" />
    

    and the actual toolbar has the same id: dashtoolbar

    <Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/dashtoolbar"
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, September 29, 2016 12:35 PM