setsupportactionbar() throws error

2019-01-28 12:01发布

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.

6条回答
Anthone
2楼-- · 2019-01-28 12:13

You can try casting it:

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

This worked for me!

查看更多
爷的心禁止访问
3楼-- · 2019-01-28 12:23

ActionBarActivity is now deprecated. Use AppCompatActivity instead

查看更多
4楼-- · 2019-01-28 12:27

You must extend your class from ActionBarActivity from support package.

查看更多
对你真心纯属浪费
5楼-- · 2019-01-28 12:31

Delete:
import android.widget.Toolbar;

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

查看更多
三岁会撩人
6楼-- · 2019-01-28 12:34

Just extend your activity to AppCompatActivity and use

import android.support.v7.widget.Toolbar;

instead of

import android.widget.Toolbar;

Hope this may help...

查看更多
我只想做你的唯一
7楼-- · 2019-01-28 12:37

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

查看更多
登录 后发表回答