无法在 NoBar.FullScreen activity 中显示 material 警报对话框

Unable to display material alert dialog in NoBar.FullScreen activity

在我的全屏 activity 中,当我尝试显示 MaterialAlertDialog 时出现以下错误。

错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jlplayer/Activity.PlayerActivity}: java.lang.IllegalArgumentException: com.google.android.material.dialog.MaterialAlertDialogBuilder requires a value for the com.example.jlplayer:attr/colorSurface attribute to be set in your app theme. You can either set the attribute in your theme or update your theme to inherit from Theme.MaterialComponents (or a descendant).
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3303)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3411)
        at android.app.ActivityThread.-wrap12(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994)
        at android.os.Handler.dispatchMessage(Handler.java:108)
        at android.os.Looper.loop(Looper.java:166)
        at android.app.ActivityThread.main(ActivityThread.java:7529)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
     Caused by: java.lang.IllegalArgumentException: com.google.android.material.dialog.MaterialAlertDialogBuilder requires a value for the com.example.jlplayer:attr/colorSurface attribute to be set in your app theme. You can either set the attribute in your theme or update your theme to inherit from Theme.MaterialComponents (or a descendant).
        at com.google.android.material.resources.MaterialAttributes.resolveOrThrow(MaterialAttributes.java:69)
        at com.google.android.material.color.MaterialColors.getColor(MaterialColors.java:64)
        at com.google.android.material.dialog.MaterialAlertDialogBuilder.<init>(MaterialAlertDialogBuilder.java:120)
        at Activity.PlayerActivity.initializePlayer(PlayerActivity.java:336)
        at Activity.PlayerActivity.onStart(PlayerActivity.java:258)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1339)
        at android.app.Activity.performStart(Activity.java:7403)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3411) 
        at android.app.ActivityThread.-wrap12(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994) 
        at android.os.Handler.dispatchMessage(Handler.java:108) 
        at android.os.Looper.loop(Looper.java:166) 
        at android.app.ActivityThread.main(ActivityThread.java:7529) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921) 

这是我显示警报对话框的方式

     new MaterialAlertDialogBuilder(PlayerActivity.this, R.style.MyAlertDialogMaterial)
                .setTitle("Test")
                .setMessage("Resume playback ")
                .setNegativeButton("Cancel", (dialog, which) -> {
                    player.seekTo(playListId, 0);
                    player.setPlayWhenReady(true);
                })
                .setPositiveButton("Resume", (dialog, which) -> {
                    player.seekTo(playListId, startPosition);
                    player.setPlayWhenReady(true);
                }).create().show();

它只是一个要显示的简单警报对话框,但我遇到了上述错误。我尝试将 colorSurface 添加到 AppTheme 它会导致其他 activity.

出现多个错误

So how do i display MaterialAlertDialog

Manifest.xml

android:theme="@style/Theme.AppCompat.Light.NoBar.FullScreen">

风格

<style name="Theme.AppCompat.Light.NoBar.FullScreen" parent="@style/Theme.AppCompat.Light">
    <!-- Full screen theme -->
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

如果我尝试 MaterialComponents 我会得到这个错误

<style name="Theme.AppCompat.Light.NoBar.FullScreen" parent="@style/Theme.MaterialComponents.Light">
        <!-- Full screen theme -->
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>

错误

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jlplayer/Activity.PlayerActivity}: android.view.InflateException: Binary XML file line #94: Binary XML file line #52: Error inflating class Button
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3303)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3411)
        at android.app.ActivityThread.-wrap12(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994)
        at android.os.Handler.dispatchMessage(Handler.java:108)
        at android.os.Looper.loop(Looper.java:166)
        at android.app.ActivityThread.main(ActivityThread.java:7529)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
     Caused by: android.view.InflateException: Binary XML file line #94: Binary XML file line #52: Error inflating class Button
     Caused by: android.view.InflateException: Binary XML file line #52: Error inflating class Button
     Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 5: TypedValue{t=0x2/d=0x7f0401ac a=4}
        at android.content.res.TypedArray.getColorStateList(TypedArray.java:569)
        at android.widget.TextView.<init>(TextView.java:1291)
        at android.widget.Button.<init>(Button.java:172)
        at android.widget.Button.<init>(Button.java:147)
        at androidx.appcompat.widget.AppCompatButton.<init>(AppCompatButton.java:74)
        at com.google.android.material.button.MaterialButton.<init>(MaterialButton.java:224)
        at com.google.android.material.button.MaterialButton.<init>(MaterialButton.java:220)
        at com.google.android.material.theme.MaterialComponentsViewInflater.createButton(MaterialComponentsViewInflater.java:43)
        at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:123)
        at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1551)
        at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1602)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:783)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:874)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:835)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:877)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:835)
        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:1006)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:870)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:835)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
        at androidx.appcompat.app.AppCompatDialog.setContentView(AppCompatDialog.java:95)
        at androidx.appcompat.app.AlertController.installContent(AlertController.java:232)
        at androidx.appcompat.app.AlertDialog.onCreate(AlertDialog.java:279)
        at android.app.Dialog.dispatchOnCreate(Dialog.java:528)
        at android.app.Dialog.show(Dialog.java:389)
        at Activity.PlayerActivity.initializePlayer(PlayerActivity.java:351)
        at Activity.PlayerActivity.onStart(PlayerActivity.java:259)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1339)
        at android.app.Activity.performStart(Activity.java:7403)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3411)
        at android.app.ActivityThread.-wrap12(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994)
2021-02-05 16:30:13.666 3666-3666/com.example.jlplayer E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:108)
        at android.os.Looper.loop(Looper.java:166)
        at android.app.ActivityThread.main(ActivityThread.java:7529)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

使用自定义对话框而不是 material,但如果您想使用

请参考这篇

https://material.io/components/dialogs/android#theming-dialogs

我从 Manifest

中删除了 android:theme="@style/Theme.AppCompat.Light.NoBar.FullScreen" 主题

我暂时解决了当前的问题。