Java 非法国家例外。由于工具栏错误,活动无法启动
我正在尝试添加一个类似于soundclouds的actionbar。我正在按照网站上关于如何自定义操作栏的说明进行操作。错误似乎是已经提供了操作栏,但我正在尝试创建另一个Java 非法国家例外。由于工具栏错误,活动无法启动,java,android,android-actionbar,Java,Android,Android Actionbar,我正在尝试添加一个类似于soundclouds的actionbar。我正在按照网站上关于如何自定义操作栏的说明进行操作。错误似乎是已经提供了操作栏,但我正在尝试创建另一个 java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set wind
java.lang.IllegalStateException: 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.
这里是onCreate():
Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
setSupportActionBar(toolbar);
addBirthdayFragment();
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
以下是布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".view.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</RelativeLayout>
结果,或者是否有其他用于选项卡式操作栏的小部件
谢谢您的帮助。您的代码是:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
Android抱怨是因为你从来没有告诉它你将使用工具栏而不是Android提供的ActionBar。要让Android知道这一点,您需要将以下内容添加到您的风格中:
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
false
真的
如果您希望有时使用Android ActionBar,有时使用工具栏,您可以将上述两行添加到扩展AppTheme的新样式中,并在AndroidManifest.xml上使用:
<!-- Use this one in the Manifest when you want to use the native ActionBar -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- your styling here -->
</style>
<!-- Use this one when you want to implement your own Toolbar -->
<style name="AppTheme.NoActionBar" parent="AppTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
假的
真的
谢谢您的帮助。干得很好,非常欢迎。请接受答案,以便其他人可以在顶部看到。感谢您将我的答案标记为正确答案:)至于ActionBar选项卡,您可以检查以下内容:
<!-- Use this one in the Manifest when you want to use the native ActionBar -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- your styling here -->
</style>
<!-- Use this one when you want to implement your own Toolbar -->
<style name="AppTheme.NoActionBar" parent="AppTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>