[英]AlertDialog won't show
I'm trying to show an alert dialog in my code. 我试图在代码中显示一个警告对话框。 It needs a Context
when it is building as its parameter. 它在构建时需要一个Context
作为其参数。 I want it to pop up in my LoginActivity
: 我希望它在我的LoginActivity
弹出:
public class LoginActivity extends ActionBarActivity
{
// Methods inside
}
But my function which generates the alertDialog
is in a separate class. 但是我生成alertDialog
函数在单独的类中。 So in order to get the appropriate Context
I tried an extra class which gives me the ApplicationContext
, which I then use as the required parameter. 因此,为了获得适当的Context
我尝试了一个额外的类,该类为我提供了ApplicationContext
,然后将其用作必需参数。
Here is my function which generates the alertDialog
: 这是我的函数,它生成alertDialog
:
public static void alertFailure(String errorTitle, String errorMessage){
Log.d("Alert Failure", "alerting failure");
AlertDialog.Builder builder = new AlertDialog.Builder(MyApplication.getAppContext()); // Needs context of loginactivity
builder.setTitle(errorTitle);
builder.setMessage(errorMessage);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
And here is my class which I use for getting the ApplicationContext
: 这是我用来获取ApplicationContext
:
public class MyApplication extends Application{
private static Context context;
public void onCreate(){
super.onCreate();
MyApplication.context = getApplicationContext();
}
public static Context getAppContext(){
return context;
}
}
When I log my program I can see that my function is running fine, but the alertDialog
doesn't pop up. 当我登录程序时,可以看到我的函数运行良好,但是没有弹出alertDialog
。 How can I pass the Context
of my LoginActivity
as the parameter? 如何传递我的LoginActivity
的Context
作为参数?
Try replacing this.. 尝试更换它。
AlertDialog alertDialog = builder.create();
alertDialog.show();
with this.. 有了这个..
builder.show();
You need an activity context for dialogs. 您需要对话框的活动上下文。 The application context won't do. 应用程序上下文不起作用。
You can pass it as a parameter to the method, eg 您可以将其作为参数传递给方法,例如
public static void alertFailure(Activity context, String errorTitle, String errorMessage){
And calling from an activity: 并从活动中调用:
AnotherClass.alertFailure(this, ...);
add another parameter as ActivityContext in the alertFailure(), then call the alertFailure("Alert Failure", "alerting failure", LoginActivity.this);
在alertFailure()中添加另一个参数作为ActivityContext,然后调用alertFailure("Alert Failure", "alerting failure", LoginActivity.this);
from LoginActivity class. 从LoginActivity类。
you can overload the method 您可以重载该方法
public static void alertFailure(Context context, String errorTitle, String errorMessage) {
Log.d("Alert Failure", "alerting failure");
AlertDialog.Builder builder = new AlertDialog.Builder(context); // Needs context of loginactivity
builder.setTitle(errorTitle);
builder.setMessage(errorMessage);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
and call it like NomeClass.alertFailure(LoginActivity.this, "title", "message");
并将其NomeClass.alertFailure(LoginActivity.this, "title", "message");
and you can keep the version without context: 并且您可以保留没有上下文的版本:
public static void alertFailure(String errorTitle, String errorMessage){
alertFailure(MyApplication.getContext(), erroTitle, errorMessage);
}
I don't think its a problem with Context
. 我认为Context
没有问题。
Call alertFailure
like this: 像这样致电alertFailure
:
new Handler().post(new Runnable() {
@Override
public void run() {
alertFailure("Title", "Message");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.