在Android的AlertDialog中显示项目时获取异常
我正在尝试创建一个包含选项的AlertDialog。我正在使用以下代码在Android的AlertDialog中显示项目时获取异常,android,exception,android-alertdialog,Android,Exception,Android Alertdialog,我正在尝试创建一个包含选项的AlertDialog。我正在使用以下代码 final CharSequence[] items={"One","two","three"}; alertDialog = new AlertDialog.Builder(this).create(); alertDialog.setTitle("Choose COlor"); alertDialog.setItems(items, new DialogI
final CharSequence[] items={"One","two","three"};
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Choose COlor");
alertDialog.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
}
});
但我不断地在编译时发现错误
方法setItems(CharSequence[],新建
类型的DialogInterface.OnClickListener(){})未定义
警报对话框
我很困惑,因为我看到的所有示例都使用此代码,那么为什么会出现此错误?
setItems
是AlertDialog.Builder
的一种方法,而不是AlertDialog
您应该在生成器上调用所有方法,而不是对话框。例如:
alertDialog = new AlertDialog.Builder(this)
.setTitle("Choose COlor")
.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
}
})
.create();
谢谢..你救了我一天:-)