Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setSupportActionBar toolbar cannot be applied to (android.widget.Toolbar) error

I've been looking for an answer and I've tried many possible solutions, but nothing seems to work..

I'm trying to setup a Material Action Bar following this tutorial.

Here's my code:

tool_bar.xml:

<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:background="@color/ColorPrimary"     android:elevation="4dp">  </android.support.v7.widget.Toolbar> 

activity.xml:

<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/drawer_layout"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="#FFFFFF">     <!-- The main content view -->     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:orientation="vertical">          <include             android:id="@+id/app_bar"             layout="@layout/tool_bar" />     </RelativeLayout>     <!-- Navigation Drawer -->     <ListView         android:id="@+id/left_drawer"         android:layout_width="220dp"         android:layout_height="match_parent"         android:layout_gravity="left"         android:background="#1C1C1C"         android:divider="@android:color/darker_gray"         android:dividerHeight="1dp" />  </android.support.v4.widget.DrawerLayout> 

And finally my activity.java:

import android.app.ActionBar; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.Toolbar;   public class rutaActivity extends ActionBarActivity {  private Toolbar toolbar;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_ruta);          getSupportActionBar().hide();//Ocultar ActivityBar anterior          toolbar = (Toolbar) findViewById(R.id.app_bar);         setSupportActionBar(toolbar); //HERE'S THE PROBLEM !!!! 

Error:

setSupporActionBar (android.support.v7.widget.Toolbar) in ActionBarActivity cannot be applied to (android.widget.Toolbar)

How can I fix this?

like image 285
HGRC Avatar asked Mar 13 '15 06:03

HGRC


People also ask

How do I set up a support action bar?

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.

What is Toolbar Android?

In Android applications, Toolbar is a kind of ViewGroup that can be placed in the XML layouts of an activity. It was introduced by the Google Android team during the release of Android Lollipop(API 21). The Toolbar is basically the advanced successor of the ActionBar.


2 Answers

In your Activity.java import android.support.v7.widget.Toolbar instead of android.widget.Toolbar:

import android.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.support.v7.widget.Toolbar;   public class rutaActivity extends AppCompactActivity {  private Toolbar toolbar; @Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_ruta);      getSupportActionBar().hide();//Ocultar ActivityBar anterior      toolbar = (Toolbar) findViewById(R.id.app_bar);     setSupportActionBar(toolbar); //NO PROBLEM !!!! 

Update:

If you are using androidx, replace

import android.support.v7.widget.Toolbar; import android.support.v7.app.AppCompatActivity; 

with newer imports

import androidx.appcompat.widget.Toolbar; import androidx.appcompat.app.AppCompatActivity; 
like image 165
Mohit Avatar answered Oct 11 '22 07:10

Mohit


In using toolbar you should extends AppCompatActivity and

import android.support.v7.widget.Toolbar  
like image 41
Kirti Chaturvedi Avatar answered Oct 11 '22 07:10

Kirti Chaturvedi