我在创建 AlertDialog 时遇到问题。
我希望当我单击一个按钮时 AlertDialog 出现,所以我将所有用于创建对话框的代码放在该按钮监听器中......相同的代码在该监听器外部工作但在监听器内部不起作用......它不起作用'调用任何错误但是当我启动模拟器时写的是关于这个的:“Aplication has terminated unexpectedly”......
这是我的代码:
btsenddata.setOnClickListener(new Button.OnClickListener() {
public void onClick (View v){
AlertDialog.Builder alt_bld = new AlertDialog.Builder(getApplicationContext() ); // I've tried with "activity1.this" instead "getApplicationContext()" but it is the same result
alt_bld.setMessage("Do you want to close this window ?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'Yes' Button
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'NO' Button
dialog.cancel();
}
});
AlertDialog alert = alt_bld.create();
// Title for AlertDialog
alert.setTitle("Title");
// Icon for AlertDialog
alert.setIcon(R.drawable.icon);
alert.show();
}});
}
....我听说 getApplicationContext() 方法会产生一些问题,但我已尝试用 activity.this 替换它,但无论如何它都不起作用... 如果有人知道这个或更多关于 alertdialogs 的原因,请回答。 谢谢。
最佳答案
"Aplication has terminated unexpectedly"
可能意味着您可以在 LogCat 中查看堆栈跟踪,它告诉您错误的行号。看看那个堆栈跟踪。如果您不明白,请通过编辑您的原始问题将其发布到此处。
关于android - AlertDialog 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2307034/