【发布时间】:2011-08-04 23:35:47
【问题描述】:
我有一个AlertDialogUtils 类,它生成一个AlertDialog,以便在发生错误时可以从任何活动中调用它。问题是我不能从createDialog() 方法中调用finish() 作为“关闭”按钮的onClickListener。
你有什么想法吗?
AlertDialogUtils 类的代码:
public class AlertDialogUtils extends Dialog {
private Context mContext;
public AlertDialogUtils(Context context) {
super(context);
mContext = context;
}
public void CreateAlertDialog(String errorMessage) {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setMessage(errorMessage)
.setCancelable(true)
.setNeutralButton("Dismiss", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mContext.finish();
//error here. Intend to close the activtiy that created this dialog and has the error
}
});
AlertDialog alert = builder.create();
alert.setOwnerActivity((Activity)mContext);
// The dialog utils is outside an activity. Need to set owner
alert.show();
}
}
【问题讨论】:
-
也许这不是最直接的方法,但你为什么不把Activity作为参数传递给
CreateAlertDialog(String)呢?
标签: android class android-activity dialog