I'm running into an odd problem. I've got an app running on a bunch of devices, but on one it fails with something seemingly simple that I can't figure out.
The Toolbar works fine on all of my devices - but is causing this error:
Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
activity_main.xml:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/header_color"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />
</com.google.android.material.appbar.AppBarLayout>
themes.xml:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.TestApp" parent="Theme.MaterialComponents.DayNight.Bridge">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" >
<item name="android:background">@color/header_color</item>
</style>
Manifest:
<activity
android:name=".MainActivity"
android:theme="@style/Theme.TestApp">
</activity>
MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Logcat Error:
2021-02-14 14:04:37.574 13026-13026/com.edtest.devicetools E/AndroidRuntime: FATAL EXCEPTION: main Process: com.edtest.devicetools, PID: 13026 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.edtest.devicetools/com.edtest.devicetools.MainActivity}: 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. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3762) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3938) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2277) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:246) at android.app.ActivityThread.main(ActivityThread.java:8425) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:596) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) Caused by: 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. at androidx.appcompat.app.AppCompatDelegateImpl.setSupportActionBar(AppCompatDelegateImpl.java:572) at androidx.appcompat.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:159) at com.edtest.devicetools.MainActivity.onCreate(MainActivity.java:54) at android.app.Activity.performCreate(Activity.java:8183) at android.app.Activity.performCreate(Activity.java:8167) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3735) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3938) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2277) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:246) at android.app.ActivityThread.main(ActivityThread.java:8425) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:596) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) 2021-02-14 14:04:37.600 13026-13026/com.edtest.devicetools I/Process: Sending signal. PID: 13026 SIG: 9
The app crashes on the setSupportActionBar EVERY time on certain devices, but not on others. All devices are running Android 10, so there's no OS difference.
I've read all of the articles about the toolbar I've tried various combinations of the parent theme with and without the action bar I can leave or remove the theme windowactionbar and windownotitle statements and seem to have the same result no matter what.
any thoughts?
Thanks!
OK - I figured out what was happening. So frustrating I missed this - and it's so crazy to figure some of this stuff out.
Themes are confusing to me for some reason - but that's for me to go figure out later.
Here's what went wrong:
New Android Studio projects have themes.xml AND also include another themes.xml (night)
I had added all of my app bar styles into the themes.xml - but I had NOT done anything to the (night) version of this file.
And the devices I was experiencing errors on were set to Dark mode in Android settings.
So the app was trying to use the alternate themes.xml and failing.
So either kill off that file if you're not ready for any dual-personality in your app or embrace it and support Dark mode
Either way - this was my issue.