我創建了一個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