The method setActionBar(Toolbar) in the type Activity is not applicable for the arguments (Toolbar)
Asked Answered
W

2

8

This is the XML for the Toolbar layout...

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mToolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</android.support.v7.widget.Toolbar>

I'm using the support library to use a Toolbar as my ActionBar. I did this in onCreate...

Toolbar mToolbar = (Toolbar)getLayoutInflater().inflate(R.layout.toolbar, null);
setActionBar(mToolbar);

But if gives me the red squigglies and tells me that message in the title. I'm like, LULWUT?!

Washin answered 14/4, 2015 at 21:18 Comment(1)
I tried inflate the layout to a View first then getting the toolbar from that view using findViewById, but that didn't work either.Washin
A
10

You probably need setSupportActionBar instead. Using the Toolbar means you should be using AppCompatActivity, and all this stuff lives in the appcompat (~support) library.

Edit: this answer still applies if you are using the now-deprecated ActionBarActivity, since ActionBarActivity inherits the setSupportActionBar method from AppCompactActivity.

Alanaalanah answered 14/4, 2015 at 21:22 Comment(0)
D
2

2019 UPDATE

According to API 29, Toolbar is now in package androidx.appcompat.widget. So, in the layout file:

<androidx.appcompat.widget.Toolbar 
    android:id="@+id/toolbar"/>

In Java code:

import androidx.appcompat.widget.Toolbar;
...
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
Delinda answered 3/10, 2019 at 10:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.