我创建了一个AlertDialog
,并将其放在Button
OnClickListener
上。 但是,该对话框正在崩溃我的应用程序。显示AlertDialog会导致我的应用程序崩溃。
我的代码有什么问题?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
你怎么在logcat中看到了什么?有什么例外? – Madushan 2012-08-12 10:11:09
当您尝试显示对话框或与其交互时,应用程序是否会崩溃?你的代码看起来没问题,它是什么,但它需要更多的上下文。我建议发布更多的Activity代码会有所帮助,至少是onCreate()和onCreateDialog()方法。另外,正如Madushan所说,logcat的内容也将为原因提供强有力的线索。 – Chilledrat 2012-08-13 10:09:08