eclipse 将工具栏设置为 ActionBar 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27412203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 22:17:37  来源:igfitidea点击:

Error setting Toolbar as ActionBar

androideclipsematerial-designandroid-toolbar

提问by BlueMango

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?

我正在尝试用工具栏替换我的应用程序的 ActionBar,但我不能,因为我不能使用 setSupportActionBar()。即使 MainActivity 扩展了 ActionBarActivity,它也会说“未定义类型 MainActivity 的 setSupportActionBar()”。我不知道该怎么办,有什么帮助吗?你需要我发布我的代码吗?

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

编辑:现在代码有效,但我的应用程序在启动时崩溃,它说:“尝试调用虚拟方法 getTitle()”。我想念什么?

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

EDIT2:一切正常我忘了我有一个片段,因此它给了我 nullPointer

回答by SANAT

Now use :

现在使用:

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

XML :

XML :

android.support.v7.widget.Toolbar

Extends :

扩展:

public class MainActivity extends AppCompatActivity

回答by Austin Musice

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

还要确保在您使用的 XML 文件中

<android.support.v7.widget.Toolbar

As your toolbar tag

作为您的工具栏标签