3

I tried following the steps for displaying a drawer above the action bar but below the status bar. The steps found here: display over the ActionBar/Toolbar and under the status bar?

But after implementing the code, the following error was thrown:
error: cannot find symbol method setSupportActionBar(Toolbar)
I've searched on Google for this exact error and not a single result.

I'm using Android Studio (1.1) and have tried using "Clean Build"
I'm using import android.support.v7.widget.Toolbar;

Toolbar toolbar = (Toolbar) findViewById(R.id.actionbar);
setSupportActionBar(toolbar); //error

Hopefully, someone recognizes this error and has a quick answer for it - otherwise I can provide more information if requested.

Community
  • 1
  • 1
Liggliluff
  • 706
  • 1
  • 6
  • 22
  • 3
    your class has to extend `ActionBarActivity` – Blackbelt Jan 26 '15 at 12:26
  • @Blackbelt and no more error is thrown - except that the app now crashes instead. But that's another error. Submit your comment as an answer, and I could accept it as the solution. – Liggliluff Jan 26 '15 at 12:30

6 Answers6

8

Edit:

Recently ActionBarActivity has been deprecated in favor of ActionBarCompat. You should use the new one, and use as the ActionBarCompat themes, E.g. Theme.AppCompat.Light

you class has to extend ActionBarActivity instead of Activity, in order to use the method setSupportActionBar

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
6

Delete:
import android.widget.Toolbar;

Add:
import android.support.v7.widget.Toolbar;

Carlos
  • 61
  • 1
  • 1
    This is what fixed it in conjunction with extending AppCompatActivity – Heath N Jun 27 '15 at 05:58
  • AppCompatActivity should be used instead. The AppcompactActivity is a class which extends FragmentActivity that is Base class for activities that use the support library action bar features. So Activity is faster than all & AppCompatActivity is the best of all. – Dung Aug 28 '18 at 18:34
5

ActionBarActivity is now deprecated. Use AppCompatActivity instead

Gaurav Kapoor
  • 51
  • 2
  • 2
  • 2
    If you're using AppCompatActivity, you also need to be using the theme "Theme.AppCompat.Light.NoActionBar" – Christina May 12 '15 at 21:34
4

You must extend your class from ActionBarActivity from support package.

anil
  • 2,083
  • 21
  • 37
0

You can try casting it:

mToolbar = (Toolbar)mFragmentView.findViewById(R.id.toolbar);
((ActionBarActivity)getActivity()).setSupportActionBar(mToolbar);

This worked for me!

Pang
  • 9,564
  • 146
  • 81
  • 122
R Lu
  • 69
  • 4
0

For me this work like thi:

import androidx.appcompat.widget.Toolbar;
Anil Kumar
  • 1,830
  • 15
  • 24