简体   繁体   English

Android对话框

[英]Android Dialogs

So I am having some difficulties with Android Dialogs. 所以我在使用Android对话框时遇到了一些困难。 All I want to do is display a dialog when a button is pressed, and then when my AsyncTask is done, dismiss it. 我要做的就是在按下按钮时显示一个对话框,然后在完成AsyncTask后将其关闭。 I can't even get it to display though. 我什至无法显示它。 Here is my code for the dialog inside my OnClick listener: 这是我的OnClick侦听器中对话框的代码:

    ProgressDialog dialog = ProgressDialog.show(getApplicationContext(), "", 
                                "Loading. Please wait...", true);

My Logcat says that it is unable to add window. 我的Logcat说它无法添加窗口。 Here it is: 这里是:

    01-21 17:44:48.044: E/AndroidRuntime(6319):     at android.app.ProgressDialog.show(ProgressDialog.java:116)

How do I get the dialog to show up? 如何显示对话框?

EDIT: 编辑:

The problem was that when I was getting the Context, it wasn't getting my Activity's context. 问题是,当我获取上下文时,并没有获取活动的上下文。 So I made a variable that was set to my Activity's context and passed that in fixing it. 因此,我创建了一个设置为我的Activity上下文的变量,并在修复它时将其传递了。

You shouldn't use getApplicationContext() for this, several people have had problems using it. 您不应该为此使用getApplicationContext(),几个人在使用它时遇到了问题。 Can you use the context of your activity one way or another? 您能否以一种或另一种方式使用活动的上下文?

You should not use application's context to initialize dialog window. 您不应使用应用程序的上下文来初始化对话框窗口。 Instead, use getBaseContext or activity's reference by using this 相反,使用getBaseContext使用或活动的参考this

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

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