material-components / material-components-android

Modular and customizable Material Design UI components for Android
Apache License 2.0
16.26k stars 3.06k forks source link

[MaterialAlertDialogBuilder] `UnsupportedOperationException` when upgrading from 1.10.0 to 1.11.0 #3981

Closed stkent closed 8 months ago

stkent commented 8 months ago

Description:

Instantiating a MaterialAlertDialogBuilder crashes after updating from library version 1.10.0 to library version 1.11.0. I am able to reproduce this on an emulator running the system image with release name UpsideDownCake, API Level 34, ABI arm64-v8a, Target Android 14.0 (Google Play), revision 12.

This is the crash:

java.lang.UnsupportedOperationException: Failed to resolve attribute at index 4: TypedValue{t=0x2/d=0x7f03013c a=8}, theme={InheritanceMap=[id=0x7f1305b8com.redacted.evergreen.app.develop:style/Redacted.Dialog.ThemeOverlay, id=0x7f1303e4com.redacted.evergreen.app.develop:style/ThemeOverlay.Material3.MaterialAlertDialog, id=0x7f13040dcom.redacted.evergreen.app.develop:style/ThemeOverlay.MaterialComponents.MaterialAlertDialog, id=0x7f130095com.redacted.evergreen.app.develop:style/Base.ThemeOverlay.MaterialComponents.MaterialAlertDialog, id=0x7f1300accom.redacted.evergreen.app.develop:style/Base.V14.ThemeOverlay.MaterialComponents.MaterialAlertDialog, id=0x7f1303b6com.redacted.evergreen.app.develop:style/ThemeOverlay.AppCompat.Dialog.Alert, id=0x7f13008acom.redacted.evergreen.app.develop:style/Base.ThemeOverlay.AppCompat.Dialog.Alert, id=0x7f130089com.redacted.evergreen.app.develop:style/Base.ThemeOverlay.AppCompat.Dialog, id=0x7f1300b6com.redacted.evergreen.app.develop:style/Base.V21.ThemeOverlay.AppCompat.Dialog, id=0x7f1300cbcom.redacted.evergreen.app.develop:style/Base.V7.ThemeOverlay.AppCompat.Dialog, id=0x7f130085com.redacted.evergreen.app.develop:style/Base.ThemeOverlay.AppCompat, id=0x7f130239com.redacted.evergreen.app.develop:style/Platform.ThemeOverlay.AppCompat], Themes=[com.redacted.evergreen.app.develop:style/Redacted.Dialog.ThemeOverlay, forced, com.redacted.evergreen.app.develop:style/Theme.Redacted.Standard, forced, com.redacted.evergreen.app.develop:style/Theme.AppCompat.Empty, forced, android:style/Theme.DeviceDefault.Light.DarkActionBar, forced]}
    at android.content.res.TypedArray.getColor(TypedArray.java:529)
    at com.google.android.material.dialog.MaterialAlertDialogBuilder.<init>(MaterialAlertDialogBuilder.java:131)

Expected behavior:

No crash, as in library version 1.10.0.

Source code:

Invocation: MaterialAlertDialogBuilder(context, 0)

<style name="Redacted.Dialog.ThemeOverlay" parent="ThemeOverlay.Material3.MaterialAlertDialog">
    <item name="buttonBarNegativeButtonStyle">@style/Redacted.Dialog.ButtonStyle.Secondary</item>
    <item name="buttonBarNeutralButtonStyle">@style/Redacted.Dialog.ButtonStyle.Primary</item>
    <item name="buttonBarPositiveButtonStyle">@style/Redacted.Dialog.ButtonStyle.Primary</item>
    <item name="materialAlertDialogBodyTextStyle">@style/Redacted.Dialog.Body.Text</item>
    <item name="materialAlertDialogTitleTextStyle">@style/Redacted.Dialog.Title.Text</item>
    <item name="alertDialogStyle">@style/Redacted.Dialog.Style</item>
    <item name="android:backgroundTint">@color/backgroundPrimaryElevated</item>
    <item name="android:backgroundDimAmount">0.56</item>
</style>

Minimal sample app repro:

I have not attempted to reproduce outside of our primary project at this time.

Android API version: 34.

Material Library version: 1.11.0.

Device: Emulator, see details in description.

~To help us triage faster, please check to make sure you are using the latest version of the library.~ ✅

Possibly related: https://github.com/material-components/material-components-android/issues/3666, though that specifically mentions usage of a beta version of Android U, which I do not believe is the case for this issue.

dsn5ft commented 8 months ago

Hi @stkent, does the issue happen on a real device running the latest production version of Android U? I wonder if that emulator image is outdated.

stkent commented 8 months ago

Hi @dsn5ft, thank you for the quick response! I tested on my personal Pixel 8 Pro running the December update of Android 34, and found that the crash still occurred there. I am currently installing the latest available system update and will report back once I've tested there too.

stkent commented 8 months ago

Also, in case it is relevant, our app's base theme is Theme.MaterialComponents.DayNight.NoActionBar, i.e. is not based on Material 3. In other locations I know we've had to add attribute values to our M2-based theme to support M3-based components; is it perhaps the case that a new attribute needs to be added to our M2 theme as a result of MDC library changes?

dsn5ft commented 8 months ago

Ah, yes. If you are using ThemeOverlay.Material3.MaterialAlertDialog then you must make sure you are using an M3 base theme (otherwise you have to add all attributes from the M3 theme to your theme to avoid attribute not found issues).

Closing the issue since that seems to definitely be what's going on, but feel free to keep the conversation going here.

stkent commented 8 months ago

OK, I think the M2 app theme is the issue here. In our dialog overlay style:

<style name="Redacted.Dialog.ThemeOverlay" parent="ThemeOverlay.Material3.MaterialAlertDialog">
    <item name="buttonBarNegativeButtonStyle">@style/Redacted.Dialog.ButtonStyle.Secondary</item>
    <item name="buttonBarNeutralButtonStyle">@style/Redacted.Dialog.ButtonStyle.Primary</item>
    <item name="buttonBarPositiveButtonStyle">@style/Redacted.Dialog.ButtonStyle.Primary</item>
    <item name="materialAlertDialogBodyTextStyle">@style/Redacted.Dialog.Body.Text</item>
    <item name="materialAlertDialogTitleTextStyle">@style/Redacted.Dialog.Title.Text</item>
    <item name="alertDialogStyle">@style/Redacted.Dialog.Style</item>
    <item name="android:backgroundTint">@color/backgroundPrimaryElevated</item>
    <item name="android:backgroundDimAmount">0.56</item>
</style>

I added a temporary backgroundTint attribute to the Redacted.Dialog.Style style:

 <style name="Redacted.Dialog.Style" parent="MaterialAlertDialog.Material3">
     <item name="shapeAppearance">@style/ShapeAppearance.Material3.Corner.ExtraLarge</item>
     <item name="shapeAppearanceOverlay">@null</item>
+    <item name="backgroundTint">@android:color/transparent</item>
 </style>

and the app no longer crashes. I think this issue can be safely closed, if you agree!

stkent commented 8 months ago

Jinx, thanks for the help here :)

dsn5ft commented 8 months ago

Np and thanks for confirming!