Material-components-android: [MaterialAlertDialogBuilder] MaterialAlertDialogBuilder crashes because of Theme

Created on 21 Apr 2020  路  9Comments  路  Source: material-components/material-components-android

Description: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.AppCompat (or a descendant).

Expected behavior: shouldn't crash

Source code: MaterialAlertDialogBuilder(activity)
.setTitle(R.string.logout_title)
.setMessage(R.string.logout_message)
.setPositiveButton(R.string.logout_positive_btn_label, null)
.setNegativeButton(R.string.logout_negative_btn_label, null)
.create()

Theme:


**Theme added on base theme**

@style/ThemeOverlay.App.MaterialAlertDialog
```

@dsn5ft any update on this?

Hi @shaikfaizal, thanks for providing the code snippets. However, it would be best if you could create a GitHub repo with a minimal app that reproduces the issue. In order for us to help it's important for us to see how everything is hooked up.

@shaikfaizal why are you explicitly settings all of these? It will default to those values but if ?attr/colorPrimary isn't defined I'm pretty sure it won't work.

<style name="ThemeOverlay.App.MaterialAlertDialog" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
       <item name="colorPrimary">?attr/colorPrimary</item>
       <item name="colorSecondary">?attr/colorSecondary</item>
       <item name="colorSurface">?attr/colorSurface</item>
       <item name="colorOnSurface">?attr/colorOnSurface</item>

<style name="Theme.MyApp" parent="Base.Theme.MyApp" />

@shaikfaizal Can you post your full theme? What is Base.Theme.MyApp ?

Was this page helpful?
0 / 5 - 0 ratings