In my app, I'm finding some users are crashing with You need to use a Theme.AppCompat theme (or descendant) with this activity
on my MainActivity's onCreate()
.
The problem is that only some users experience this (and not all the time) and I'm unable to reproduce it.
There are two main things which I've checked:
- Ensure all activities have a theme parent that inherits from an AppCompat theme
- Ensure all Alert Dialogs use
activity context
and notapplication context
when creating them
None of these things worked. Here is a sample of my manifest.
<application
android:allowBackup="false"
tools:replace="android:allowBackup,android:label"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:largeHeap="true"
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/MyTheme">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="MainActivity"
android:screenOrientation="portrait"
android:launchMode="singleTop">
<style name="MyTheme" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">
<item name="android:windowBackground">@android:color/white</item>
<item name="colorControlNormal">@color/color_control_normal</item>
<item name="colorPrimary">@color/red</item>
<item name="colorPrimaryDark">@color/dark_redr</item>
<item name="colorAccent">@color/blue</item>
</style>
class MainActivity : AppCompatActivity()
I believe Theme.MaterialComponents.Light.NoActionBar.Bridge
descends from an AppCompat theme (https://material.io/develop/android/docs/getting-started#bridge-themes).
I've also tried to put android:theme="@style/MyTheme"
specifically into the MainActivity
activity tags in the manifest and that didn't work.
I could try to use AppCompat
specifically in my theme, but I am worried about the unintended side effects with changing the parent's theme from a Material Design theme to a normal one.
Here's a stacktrace:
Fatal Exception: java.lang.RuntimeException
Unable to start activity ComponentInfo{com.package/com.package.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3654)
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3806)
android.app.ActivityThread.handleRelaunchActivityInner (ActivityThread.java:5802)
android.app.ActivityThread.handleRelaunchActivity (ActivityThread.java:5710)
android.app.servertransaction.ActivityRelaunchItem.execute (ActivityRelaunchItem.java:69)
android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:135)
android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:95)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:2267)
android.os.Handler.dispatchMessage (Handler.java:107)
android.os.Looper.loop (Looper.java:237)
android.app.ActivityThread.main (ActivityThread.java:8167)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:496)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1100)
androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor (AppCompatDelegateImpl.java:846)
androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor (AppCompatDelegateImpl.java:809)
androidx.appcompat.app.AppCompatDelegateImpl.setContentView (AppCompatDelegateImpl.java:696)
androidx.appcompat.app.AppCompatActivity.setContentView (AppCompatActivity.java:195)
androidx.databinding.DataBindingUtil.setContentView (DataBindingUtil.java:303)
androidx.databinding.DataBindingUtil.setContentView (DataBindingUtil.java:284)
com.package.MainActivity.onCreate (MainActivity.kt:134)
android.app.Activity.performCreate (Activity.java:7963)
android.app.Activity.performCreate (Activity.java:7952)
android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1307)
android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3629)
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3806)
android.app.ActivityThread.handleRelaunchActivityInner (ActivityThread.java:5802)
android.app.ActivityThread.handleRelaunchActivity (ActivityThread.java:5710)
android.app.servertransaction.ActivityRelaunchItem.execute (ActivityRelaunchItem.java:69)
android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:135)
android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:95)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:2267)
android.os.Handler.dispatchMessage (Handler.java:107)
android.os.Looper.loop (Looper.java:237)
android.app.ActivityThread.main (ActivityThread.java:8167)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:496)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1100)
Line 134 of MainActivity
is this binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
CodePudding user response:
I have faced some issues like this. With some view components that require this, you need to specify the theme for current activity as Theme.AppCompat. Maybe you are using its descendant but just try the original.
CodePudding user response:
Have you tried to inflate using the generated binding class
, instead of using DataBindingUtil
, as discussed here?