Error setting Toolbar as ActionBar

11,663

Solution 1

Without code, my only recommendation, is making sure your import statement is as follows:

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

Also make sure in your XML files that you are using

<android.support.v7.widget.Toolbar

As your toolbar tag

Solution 2

Now use :

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
setSupportActionBar(toolbar);

XML :

android.support.v7.widget.Toolbar

Extends :

public class MainActivity extends AppCompatActivity
Share:
11,663
MattButtMatt
Author by

MattButtMatt

["hip","hip"] (hip hip array!) Sorry.

Updated on June 28, 2022

Comments

  • MattButtMatt
    MattButtMatt almost 2 years

    I'm trying to replace the ActionBar of my app with a Toolbar but I can't because I can't use setSupportActionBar(). It says "setSupportActionBar() is undefined for the type MainActivity" even if MainActivity extends ActionBarActivity. I don't know what to do, any help? Do you need me to post my code?

    EDIT: Now the code works but my app crashes on start and it says: "Attempt to invoke virtual method getTitle()". What do I miss?

    EDIT2: Everything works I forgot I had a Fragment and because of that it gave me nullPointer

  • MattButtMatt
    MattButtMatt over 9 years
    The only thing I'm missing is import android.support.v7.widget.Toolbar; because I have import android.widget.Toolbar; If I try to add it, it says "The import android.support.v7.widget.Toolbar; cannot be resolved". I don't know why, I use the appcompat_v7 library.
  • jyomin
    jyomin almost 9 years
    @BlueMango do you find the solution of your problrm why it is not importing import android.support.v7.widget.Toolbar;?
  • Rethinavel
    Rethinavel almost 9 years
    @BlueMango. I am facing the same issue.. Could you please share how you did that?
  • raddevus
    raddevus over 8 years
    This is actually the complete and definitive answer, because you show the extends AppCompatActivity. Thanks
  • CEO tech4lifeapps
    CEO tech4lifeapps almost 3 years
    The question was asked more than 6 years ago. It would be interesting to know if your suggestion using androidx works.