AppCompatActivity类中的getSupportActionBar无法应用于android.support.v7.widget.Toolbar

AppCompatActivity类中的getSupportActionBar无法应用于android.support.v7.widget.Toolbar,android,android-actionbar,material-design,android-appcompat,Android,Android Actionbar,Material Design,Android Appcompat,事实上,我在将Android应用程序改为material design时遇到了一个问题 我尝试调用getSupportActionbar,但总是收到错误消息 getSupportActionBar in class AppCompatActivity cannot be applied to android.support.v7.widget.Toolbar 我的代码是 import android.support.v7.app.AppCompatActivity; import androi

事实上,我在将Android应用程序改为material design时遇到了一个问题

我尝试调用getSupportActionbar,但总是收到错误消息

getSupportActionBar in class AppCompatActivity cannot be applied to android.support.v7.widget.Toolbar
我的代码是

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mToolbar = (Toolbar) findViewById(R.id.toolbar);

    getSupportActionBar(mToolbar);

    getSupportActionBar().setDisplayShowHomeEnabled(true);
}
该错误在getSupportActionBar(mToolbar)上抛出

工具栏在Toolbar.xml中声明

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />


希望你们中的一些人能帮助我

必须是设置支持操作栏(mToolbar)而不是获取支持操作栏(mToolbar)

添加到您的活动xml布局代码如下:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    />
希望有此帮助。

如果(工具栏!=null) {

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true); // or other
}


这也适用于我,我收到了一个空指针引用。谢谢你

你的意思是:
setSupportActionBar(mToolbar)
?难以置信,我已经找了好几个小时找这个错误-。-thx一个lot在
Ctrl+v
:)的时候一定要小心
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true); // or other