Android 错误:'java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor.' 解决方法
这个错误是因为你的 Activity 已经有一个 ActionBar 了,但是你又请求了一个 ActionBar,导致冲突。解决方法如下:
- 在你的 Activity 的 onCreate() 方法中,设置一个 toolbar 代替 ActionBar,代码如下:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
- 在你的 Manifest 文件中,为该 Activity 设置一个主题,将 windowActionBar 设置为 false,代码如下:
<activity android:name=".YourActivity"
android:theme="@style/AppTheme.NoActionBar">
</activity>
- 在你的 styles.xml 文件中,为该主题设置一个 NoActionBar 的样式,代码如下:
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
这样就可以解决该错误了。
原文地址: https://www.cveoy.top/t/topic/nRlR 著作权归作者所有。请勿转载和采集!