Android 如何显示MaterialAlertDialog?
我正在将我的应用程序的主题从Android 如何显示MaterialAlertDialog?,android,themes,android-alertdialog,material-components,Android,Themes,Android Alertdialog,Material Components,我正在将我的应用程序的主题从AppCompat更改为MaterialComponents,除警报对话外,所有内容都正常工作。我已经在样式和清单中将应用程序主题设置为MaterialComponents 风格 <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar"> <item name="colorPrimary">@color/grey</item>
AppCompat
更改为MaterialComponents
,除警报对话外,所有内容都正常工作。我已经在样式和清单中将应用程序主题设置为MaterialComponents
风格
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">@color/grey</item>
<item name="colorPrimaryDark">@color/black</item>
<item name="colorAccent">@color/red</item>
<item name="colorSurface">@color/white</item>
<item name="colorOnSecondary">@color/white</item>
<item name="colorSecondary">@color/red</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar"/>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.MaterialComponents.Light"/>
AndroidManifest
<application
android:allowBackup="true"
android:fullBackupContent="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
最后,我得到的错误是
java.lang.IllegalArgumentException:此组件上的样式要求应用程序主题为theme.AppCompat(或其后代)
如何修复此错误?显示对话框的函数将
上下文
作为其输入参数,而该参数又是注入函数所在类的应用程序上下文
通过将函数中的输入参数类型更改为
活动
,我成功地解决了这个问题,这是一种执行正确主题的方法。您使用的是哪个版本的材质组件?1.1.0。只是把它包括在问题里
<application
android:allowBackup="true"
android:fullBackupContent="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
MaterialAlertDialogBuilder(context)
.setTitle(R.string.tip_title)
.setMessage(R.string.tip)
.setNeutralButton(R.string.ok, null)
.setPositiveButton(R.string.do_not_show_again) { _, _ ->
preferencesHelper.disableTip()
}.show()