应用toolbar报错This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
时间: 2024-02-20 12:00:00 浏览: 20
这个错误是因为在应用的主题中同时启用了ActionBar和Toolbar,解决方法是在主题中将windowActionBar设为false,这样就可以使用Toolbar了。
例如,在你的主题中添加以下代码:
```
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
```
或者,你可以在Activity的onCreate()方法中添加以下代码:
```
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle("My Toolbar");
}
```
这样就可以在不使用ActionBar的情况下使用Toolbar了。