简体   繁体   English

无法显示来自 android 应用程序的 AlertDialog class

[英]Unable to display an AlertDialog from the android Application class

I'm trying to display programmatically an AlertDialog from the Application class using the applicationContext.我正在尝试使用 applicationContext 以编程方式显示来自应用程序 class 的 AlertDialog。 I'm using my own Theme that extends Theme.Material3.Light .我正在使用我自己的扩展Theme.Material3.Light的主题。 Only Toast messages works fine.只有 Toast 消息可以正常工作。 I'm getting this error:我收到此错误:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. java.lang.IllegalStateException:您需要在此活动中使用 Theme.AppCompat 主题(或后代)。

My application Theme is:我的申请主题是:

android:theme="@style/Theme.MyTheme"

MyTheme is defined as:我的主题定义为:

<style name="Theme.MyTheme" parent="Theme.Material3.Light">

I have also a custom style for AlertDialogs defined in MyTheme:我还为 MyTheme 中定义的 AlertDialogs 自定义样式:

<!-- AlertDialog Style-->
<style name="AlertDialogTheme" parent="MaterialAlertDialog.Material3">
    <item name="android:background">@drawable/dialog_light</item>
</style>

The class from which I launch the event我发起活动的 class

class AppController: Application()

I tried changing the default theme in the Manifest and then setting the custom one programmatically when the application in launched but it did not work.我尝试更改 Manifest 中的默认主题,然后在应用程序启动时以编程方式设置自定义主题,但它不起作用。

It seems like your activity extends from AppCompatActivity and your theme is from Activity.看起来你的活动是从 AppCompatActivity 延伸出来的,而你的主题是从 Activity 来的。

In your style.xml you have to do something like this:在你的 style.xml 你必须做这样的事情:

<style name="ActionBarAppCompatTheme" parent="Theme.AppCompat.Light.DarkActionBar">

Or you can just change AppCompatActivity to Activity in your class.或者您可以将 AppCompatActivity 更改为 class 中的 Activity。

Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM