我已经构建了一个辅助类来存储各种AlertDialog类型。我认为这将有用,以便我可以在我的代码中的任何地方调用它们。不幸的是,我在new AlertDialog.Builder()
处收到了错误。它说Cannot resolve constructor `Builder()
。我怎样才能让它发挥作用?
public class AlertDialogHelper {
public void showAboutDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder();
builder.setTitle(R.string.about);
builder.setMessage("A weather app made by Martin Erlic")
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Ok
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
在我的活动中:
private void showAboutAlertDialog() {
AlertDialogHelper alertDialogHelper = new AlertDialogHelper();
alertDialogHelper.showAboutDialog();
}
答案 0 :(得分:1)
你应该在构造函数中传递一个Context,如下所示:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
来自您的活动:
alertDialogHelper.showAboutDialog(this);
现在:
public void showAboutDialog(Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
....
答案 1 :(得分:0)
我是这样做的:
public class AlertDialogHelper {
public static Dialog CreateDialog(Context mContext) {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle(R.string.about);
builder.setMessage("A weather app made by Martin Erlic")
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Ok
}
});
return builder.create();
}
}
在我的活动中:
AlertDialogHelper.CreateDialog(this).show();