Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?!

like image 854
Adam Komar Avatar asked Apr 14 '15 21:04

Adam Komar


People also ask

How do I set a support action bar in activity?

To use the ActionBar utility methods, call the activity's getSupportActionBar() method. This method returns a reference to an appcompat ActionBar object. Once you have that reference, you can call any of the ActionBar methods to adjust the app bar. For example, to hide the app bar, call ActionBar.

How to add Action bar to activity in Android?

Right-click on the res folder and selects New -> Directory. Give the name “menu” to the new directory. Further, create a new Menu Resource File by right click on the menu directory. As the ActionBar is being created for the main Activity, type the name as “main” to the Menu Resource File.

What is Androidx Appcompat widget toolbar?

androidx.appcompat.widget.Toolbar. A standard toolbar for use within application content. A Toolbar is a generalization of action bars for use within application layouts.


2 Answers

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.

like image 121
stkent Avatar answered Sep 30 '22 19:09

stkent


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));
like image 26
akelec Avatar answered Sep 30 '22 18:09

akelec