简体   繁体   中英

Android action bar crashes with theme

I use Google's support libraries of version 23.1.0 .

compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:recyclerview-v7:23.1.0'
compile 'com.android.support:cardview-v7:23.1.0'
compile 'com.android.support:preference-v7:23.1.0'
compile 'com.android.support:design:23.1.0'

I am reusing a theme file that worked with versions of the support libraries prior to the introduction of material design.

<style name="AppTheme.StatusBarOverlay.Main" parent="@style/AppTheme.Main">
    <item name="android:windowContentOverlay">@null</item>
</style>
<style name="AppTheme.Main" parent="@style/AppBaseTheme.Main">
    <item name="windowActionBar">false</item>
</style>
<style name="AppBaseTheme.Main" parent="@style/Theme.AppCompat.Main">
    <item name="colorPrimary">@color/main_color</item>
    <item name="colorPrimaryDark">@color/main_color_dk</item>
</style>
<style name="Theme.AppCompat.Main" parent="@style/Base.Theme.AppCompat.Main" />
<style name="Base.Theme.AppCompat.Main" parent="@style/Base.V7.Theme.AppCompat" />
<style name="Theme.CustomDialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowBackground">@drawable/trans_draw</item>
    <item name="android:windowNoTitle">true</item>
</style>

I made my MainActivity use the style by setting the theme in the manifest

    <activity
        android:name=".activities.MainActivity"
        android:label="@string/title_activity_main"
        android:theme="@style/AppTheme.StatusBarOverlay.Main" >
    </activity>

When the action bar was retrieved

ActionBar actionBar = getSupportActionBar();

the following exception was raised.

Caused by: java.lang.IllegalArgumentException: AppCompat does not support the current theme features: { windowActionBar: false, windowActionBarOverlay: false, android:windowIsFloating: false, windowActionModeOverlay: false, windowNoTitle: false }
            at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:423)
            at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:280)
            at android.support.v7.app.AppCompatDelegateImplV7.initWindowDecorActionBar(AppCompatDelegateImplV7.java:173)
            at android.support.v7.app.AppCompatDelegateImplBase.getSupportActionBar(AppCompatDelegateImplBase.java:89)
            at android.support.v7.app.AppCompatActivity.getSupportActionBar(AppCompatActivity.java:79)
            at com.dynamicwebapac.commerce.mobile.custom.CustomActivity.setupActionBar(CustomActivity.java:39)
            at com.dynamicwebapac.commerce.mobile.custom.CustomActivity.onCreate(CustomActivity.java:25)
            at com.dynamicwebapac.commerce.mobile.activities.MainActivity.onCreate(MainActivity.java:181)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

What happened to the Android support library after material design was introduced? How can I solve my current problem?

Using the new material support library, we set up themes like this styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

Also in the manifest

android:theme="@style/AppTheme"

And then we use the Toolbar as the action bar .Do this in your parent layout

<android.support.v7.widget.Toolbar 
   android:id="@+id/toolbar"
   android:layout_width="match_parent"
   android:layout_height="?attr/actionBarSize"
   android:background="?attr/colorPrimary" />

And we set it up in our Java source using

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

Don't worry it happens with most of us. I'd like to start with reason of this problem.

The Material Design is in the favour of using Toolbar which is more flexible,...etc hence the themes or styles provided by AppCompat library are without an ActionBar or you can say a support ActionBar is provided and hence e need to disable the system default ActionBar .

Now follow any of the 2 steps below as suggested by Colns Abt :

  1. Add any style which has NoActionBar at the ending like Theme.AppCompat.NoActionBar

    OR

  2. Add the following lines in your custom style

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

Further follow steps given by Colns Abt .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM