从标准Ja​​va类返回AlertDialog

时间:2011-12-31 16:50:32

标签: android android-alertdialog

我目前正在开发一个Android项目。我正在尝试在标准java类中进行警报对话,以便可以在整个应用程序中重用代码。

但是,它会将类中的alertdialog返回给活动,但是当我尝试显示警告对话框时,它会显示以下错误:

  

无法添加窗口 - 令牌null不适用于应用程序

以下是我用于创建警报对话的代码

public AlertDialog showAlertDialog(String message, Context context) 
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage("hello")
            .setCancelable(false)
            .setPositiveButton("Yes", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
        AlertDialog alert = builder.create();
        return alert;
    }

以下是我正在尝试显示警告对话框的Android活动的代码

Common cla = new Common();
AlertDialog alert = cla.showAlertDialog("Hello", getApplicationContext());
alert.show();

Common是类的名称

1 个答案:

答案 0 :(得分:1)

请将您的AlertDialog创建逻辑更改为AlertDialog.Builder builder = new AlertDialog.Builder(yourActivity.this);

相关问题