Search code examples
androidmaterial-designandroid-alertdialogandroid-stylesmaterial-components-android

Android: AlertDialog with custom style in MaterialTheme throws resource not found exception


I have the theme Theme.MaterialComponents.Light.NoActionBar in my styles as the main Apptheme.

My alert theme is

<style name="MyDialogTheme" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
        <item name="android:windowBackground">@drawable/alert_bg</item>
        <item name="android:textColor">@color/text</item>
    </style>

I build my alert dialogue like this

AlertDialog.Builder builder = new AlertDialog.Builder(AllowanceActivity.this, R.style.MyDialogTheme);

when I run it without the style it runs properly but it crashes if I use any other style I've tried with all the themes extending alert Theme.AppCompat.Light.Dialog.Alert and Theme.AppCompat.Dialog.Alert and Theme.MaterialComponents.Dialog.Alert but I always get the error.

android.content.res.Resources$NotFoundException: Resource ID #0x7f07005d
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2462)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2522)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5471)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)
     Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f07005d
        at android.content.res.Resources.getValue(Resources.java:1432)
        at android.content.res.Resources.getValue(Resources.java:1412)
        at androidx.appcompat.widget.ResourceManagerInternal.loadDrawableFromDelegates(ResourceManagerInternal.java:252)
        at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:139)
        at androidx.appcompat.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:411)
        at androidx.appcompat.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:86)
        at androidx.appcompat.app.AppCompatDelegateImpl.attachToWindow(AppCompatDelegateImpl.java:637)
        at androidx.appcompat.app.AppCompatDelegateImpl.<init>(AppCompatDelegateImpl.java:311)
        at androidx.appcompat.app.AppCompatDelegateImpl.<init>(AppCompatDelegateImpl.java:272)
        at androidx.appcompat.app.AppCompatDelegate.create(AppCompatDelegate.java:232)
        at androidx.appcompat.app.AppCompatDialog.getDelegate(AppCompatDialog.java:170)
        at androidx.appcompat.app.AppCompatDialog.<init>(AppCompatDialog.java:59)
        at androidx.appcompat.app.AlertDialog.<init>(AlertDialog.java:98)
        at androidx.appcompat.app.AlertDialog$Builder.create(AlertDialog.java:983)

is anyone having the same problem or am I missing something


Solution

  • Use the MaterialAlertDialogBuilder which configures the instantiated AlertDialog with Material specs and theming.

    Something like:

    new MaterialAlertDialogBuilder(context,
                     R.style.MyThemeOverlay_MaterialComponents_MaterialAlertDialog)
                .setTitle("Dialog")
                //...
                .show();
    

    Also use this parent style ThemeOverlay.MaterialComponents.MaterialAlertDialog.