Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android的AlertDialog中显示项目时获取异常_Android_Exception_Android Alertdialog - Fatal编程技术网

在Android的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

我正在尝试创建一个包含选项的AlertDialog。我正在使用以下代码

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();

谢谢..你救了我一天:-)