试图移动我的东西使用工具栏而不是操作栏,但我一直得到一个错误说

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tyczj.weddingalbum/com.xxx.xxx.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5039)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
            at android.support.v7.app.ActionBarActivityDelegateBase.setSupportActionBar(ActionBarActivityDelegateBase.java:165)
            at android.support.v7.app.ActionBarActivity.setSupportActionBar(ActionBarActivity.java:92)
            at com.xxx.xxx.MainActivity.onCreate(MainActivity.java:113)
            at android.app.Activity.performCreate(Activity.java:5104)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5039)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)

然后我添加了我的样式,让我的活动没有动作栏

<style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light">
        <item name="android:windowActionBar">false</item>
</style>

这个主题适用于我清单中的活动

<activity
        android:name=".MainActivity"
        android:windowSoftInputMode="adjustResize|stateHidden"
        android:theme="@style/AppCompatTheme" android:screenOrientation="portrait"/>

MainActivity扩展了GooglePlayServiceActivity,所以我也设置了主题

<activity
       android:name=".GooglePlayServicesActivity"
       android:label="@string/title_activity_google_play_services"
       android:theme="@style/AppCompatTheme">

但我还是得到了错误。我也不要求窗口功能的任何地方。知道为什么我还能收到这个吗?


我认为你是在为Android Lollipop开发游戏,但不管怎样,你都应该包含以下内容:

<item name="windowActionBar">false</item> 

到app/src/main/res/values/styles.xml中的主题声明。

另外,如果你使用的是22.1或更高版本的AppCompatActivity支持库,添加这一行:

<item name="windowNoTitle">true</item>

在添加了这些内容之后,你的主题声明可能是这样的:

<!-- Base application theme. -->
<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>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

另一个简单的方法是让你的主题成为theme . appcompat . light . noactionbar的子元素,就像这样:

<style name="NoActionBarTheme" parent="Theme.AppCompat.Light.NoActionBar">
     ...
</style>

转到项目的'style.xml',并使windowActionBar为false

<style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light">
        <item name="android:windowActionBar">false</item>
</style>

我也面临着同样的问题。但是我用了:

getSupportActionBar () hide ();

之前

setContentView (R.layout.activity_main);

现在它正在起作用。

我们可以在style。xml中尝试其他选项,

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

将其添加到values/styles.xml中

<style name="YourCustomTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>

<style name="AppBaseTheme" parent="YourCustomTheme">

</style>

<style name="AppTheme" parent="AppBaseTheme">

</style>

并在values-v11/styles.xml和values-v14/styles.xml中添加以下代码

<style name="AppBaseTheme" parent="YourCustomTheme">

</style>

这是它。它会起作用的。

要使用工具栏作为操作栏,首先禁用装饰提供的操作栏。

最简单的方法是让你的主题从

Theme.AppCompat.NoActionBar

(或其轻型变种)。

其次,创建一个工具栏实例,通常通过你的布局XML:

<android.support.v7.widget.Toolbar
    android:id=”@+id/my_awesome_toolbar”
    android:layout_height=”wrap_content”
    android:layout_width=”match_parent”
    android:minHeight=”?attr/actionBarSize”
    android:background=”?attr/colorPrimary” />

然后在你的活动或片段中,设置工具栏作为你的动作栏:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.blah);

    Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
    setSupportActionBar(toolbar);
}

这个代码对我有用。

如果你想把一些活动和动作栏结合起来,而另一些没有,你应该使用启用了动作栏的基本主题,然后创建一个子主题,你将在不需要动作栏的活动上使用它

例如,你可以像这样使用子样式

             <style name="AppTheme.NoActionBar">
               <item name="windowActionBar">false</item>
               <item name="windowNoTitle">true</item>
            </style>

而基本主题扩展说

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

然后在AndroidManifest文件的activity标签中使用非actionbar主题

   <activity
        android:name="com.example.NonActionBarActivity"
        android:theme="@style/AppTheme.NoActionBar"

您必须将此应用于每个不需要操作栏的单独活动,因此如果您的项目需要的操作栏活动比不需要的活动少,那么最好将此应用于基本主题级别

添加单行android:theme="@style/AppTheme。NoActionBar"到AndroidManifest中的activity,这样就完成了。


AndroidManifest.xml:

<activity android:name=".activity.YourActivity"
          android:theme="@style/AppTheme.NoActionBar"><!-- ADD THIS LINE -->

styles.xml

<style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
</style>

如果您正在使用Appcompact Activity,请在主题中使用这三行。

<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowActionBarOverlay">false</item>

在你的应用主题中添加这两行,位于style.xml:-

 <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>

    //Add this two line will fix your problem
    <item name="windowNoTitle">true</item>   <--1(this applies after 22.0.1 support library i think 
    <item name="windowActionBar">true</item> <--2

我通过删除这条线来解决它:

android:主题=“@style /主题。MyCompatTheme”

从Manifest文件中的活动属性

简单地说,你可以做到以下几点:-

if (android.os.Build.VERSION.SDK_INT >= 21) {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));

    }

这就是我解决问题的方法。在你的AndroidMainfest.xml中添加以下代码

<activity android:name=".YourClass"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar">
 </activity>

你需要改变

  <activity
        android:name=".YOUR ACTIVITY"
        android:theme="@style/AppTheme.NoActionBar" />
</application>`

清单上的这些行。这对我来说非常合适。

我在xml中添加了工具栏。然后在我的活动中,我添加了这句话:

setSupportActionBar(toolbar);

去掉这个对我很有效。我希望它能帮助到一些人。

只需输入parent="Theme.AppCompat. "你的style.xml文件中的NoActionBar

<style parent="Theme.AppCompat. "NoActionBar AppTheme“name =”。NoActionBar”>

如果你在这一行得到错误:

setSupportActionBar(...);

您需要检查您的活动是否引用了包含工具栏的主题。应用程序的AppTheme可能已经包含工具栏,例如

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

你还想再加一个。如果您想使用应用程序的AppTheme,则需要从manifest.xml文件中的活动中删除主题。

例如:

<activity
    android:name=".ui.activities.SettingsActivity"
    android:theme="@style/AppTheme1" /> --> remove this theme

在manifest或Activity标签中使用它。

android:主题=“@style / AppTheme。NoActionBar”

 <style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light">
            <item name="android:windowActionBar">false</item>
    </style>

    Change this to NoActionBar 

        <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
            <!-- Customize your theme here. -->

        </style>

    This one worked for me

或者简单地改变@Style/AppTheme

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">

这对我很管用!

由于内部的自定义属性,我得到了这个错误,删除它修复了这个问题。

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
..
        <item name="searchBarBgColor">#383838</item>  --> remove this line
    </style>

这也可能是因为你的样式没有指定父样式:

<style name="DarkModeTheme" >
    <item name="searchBarBgColor">#D6D6D6</item>
</style>

删除它,它应该能解决问题。

这很简单,你只需要迈出两步中的任何一步。

将设备显示模式从暗模式更改为正常模式或。 在style.xml中,如果你使用的是AppCompat,那么在你的theme/style中添加这两行。<item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> .

尝试更改应用程序主题的父元素

在themes.xml中更改如下

<style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light">

to

<style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">

或 下面是在主题定义中禁用操作栏的正确方法

    <style name="AppCompatTheme" parent="@style/Theme.AppCompat.Light">    
    <item name="windowActionBar">false</item>
                <item name="windowNoTitle">true</item>
</style>

这里给出的很多答案都是正确的。我的组织重写。

如果你显式地添加appbar/actionbar到你的应用程序,那么你需要关闭默认的一个。 如果你想使用像这样的元素

<com.google.android.material.appbar.AppBarLayout>
or 
<android.support.v7.widget.Toolbar>

然后你需要使用正确的样式关闭隐式操作栏。

首先是查看应用程序的根主题(androidmanifest.xml标记中提到的主题)。

<application
       xer_main_round"
        android:supportsRtl="true"
        android:theme="@style/myRootTheme">
-----
then your style.xml or theme.xml or similar file should have style that turns off default actionbar. 

 <style name="myRootTheme" parent="Theme.AppCompat.Light.NoActionBar">

或者您可以使用显式样式将其从根主题中隐藏。

<item name="windowActionBar">false</item> 

您可能需要查看应用程序栏文档 https://developer.android.com/training/appbar/setting-up