繁体   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();
}

这是我正在使用的方法,我不明白它有什么问题。 我什至用您要添加的通用代码替换了null ,但是该框仍然不会弹出。 有什么建议么?

尝试:

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

也许您不是从实际上是Activity的类中调用它

通过上下文

问题是您没有在创建AlertDialog对象,而是在使用AlertBuilder对象显示警报对话框。

更新

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