[英]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.