简体   繁体   English

AlertDialog没有出现

[英]AlertDialog doesn't show up

public void MsgBox(String title, String msg){

    AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);

    dlgAlert.setMessage("The message");
    dlgAlert.setTitle("Titel");

    dlgAlert.setPositiveButton("OK", null);

    dlgAlert.create().show();
}

This is the method I am using, I don't understand what is wrong with it. 这是我正在使用的方法,我不明白它有什么问题。 I've even replaced null with the common code you would add, but the box still won't pop up. 我什至用您要添加的通用代码替换了null ,但是该框仍然不会弹出。 Any suggestions? 有什么建议么?

try: 尝试:

 new AlertDialog.Builder(this.getContext());

maybe you're not calling it from a class that's actually an Activity 也许您不是从实际上是Activity的类中调用它

Pass the context 通过上下文

Issue was that you are not creating AlertDialog object and you are showing alert dialog using AlertBuilder object. 问题是您没有在创建AlertDialog对象,而是在使用AlertBuilder对象显示警报对话框。

Update 更新

public void MsgBox(String title, String msg, Context context){
        AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(context);
        dlgAlert.setMessage("The message");
        dlgAlert.setTitle("Titel");
        dlgAlert.setPositiveButton("OK", null);

            AlertDialog alertDialog = dlgAlert.create();

            // show it
            alertDialog.show();
    }

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

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