This Activity already has an action bar supplied by the window decor

后端 未结 21 2268
-上瘾入骨i
-上瘾入骨i 2020-11-21 23:45

Trying to move over my stuff to use Toolbar instead of action bar but I keep getting an error saying

java.lang.RuntimeException: Unable to start         


        
相关标签:
21条回答
  • 2020-11-22 00:39

    If you want to combine some activities with actionbar and others not, you should use the base theme have actionbar enabled and then create a sub theme that you gonna use it on activities that don't require actionbar

    For example you can use a sub style like this

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

    While the base theme extends say

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

    and then use the non actionbar theme in the AndroidManifest File within the activity tag say

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

    you must apply this to each individual activity that don't need actionbar so if your project requires fewer action bar activities than non, then it's better to apply this on the base theme level

    0 讨论(0)
  • 2020-11-22 00:42

    I think you're developing for Android Lollipop, but anyway include this line:

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

    to your theme declaration inside of your app/src/main/res/values/styles.xml.

    Also, if you're using AppCompatActivity support library of version 22.1 or greater, add this line:

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

    Your theme declaration may look like this after all these additions:

    <!-- 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>
    
    0 讨论(0)
  • 2020-11-22 00:42

    Add single line android:theme="@style/AppTheme.NoActionBar" to activity in AndroidManifest and you've done.


    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>
    
    0 讨论(0)
  • 2020-11-22 00:43

    I also faced same problem. But I used:

    getSupportActionBar().hide();

    before

    setContentView(R.layout.activity_main);

    Now it is working.

    And we can try other option in 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>
    
    0 讨论(0)
  • 2020-11-22 00:44

    If you are using Appcompact Activity use these three lines in your theme.

    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowActionBarOverlay">false</item>
    
    0 讨论(0)
  • 2020-11-22 00:45
     <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
    
    0 讨论(0)
提交回复
热议问题