setSupportActionBar无法应用于android.support.v7.widget.Toolbar [英] setSupportActionBar cannot be applied to android.support.v7.widget.Toolbar
问题描述
在这个新项目上,将工具栏设置为主活动Java文件时,它会在
On this new project, when setting a toolbar into the main activity java file, it gets the error on
"setSupportActionBar(mToolbar)"中说"getSupportActionBar()" AppCompatActivity无法应用于 (android.support.v7.widget.Toolbar)"
"setSupportActionBar(mToolbar)" saying "getSupportActionBar() in AppCompatActivity cannot be applyed to (android.support.v7.widget.Toolbar)"
在类似的问题上,答案只是从已设置的导入android.widget.Toolbar"更改为导入android.support.v7.widget.Toolbar". 主要活动xml是这样的:
On similar questions, the answers was just to change from "import android.widget.Toolbar" to "import android.support.v7.widget.Toolbar" which is already set. The main activity xml is this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
tools:context=".MainActivity">
<include
android:id="@+id/main_page_toolbar"
layout="@layout/app_bar_layout"
>
</include>
</RelativeLayout>
我要插入的工具栏是:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_app_bar"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
以及主要的Java文件:
And the main java file:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = findViewById(R.id.main_page_toolbar);
getSupportActionBar(mToolbar);
getSupportActionBar().setTitle("WhatsApp");
}
}
我错过了什么吗?兼容性集?
Am I missing something? A compatibility set?
推荐答案
看起来像是一个错字,您正在呼叫getSupportActionBar()
(带有g
)而不是setSupportActionBar()
.
Looks like it's a typo, and you're calling getSupportActionBar()
(with a g
) instead of setSupportActionBar()
.
这篇关于setSupportActionBar无法应用于android.support.v7.widget.Toolbar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!