Java 单选按钮alertDialog中的ArrayIndexOutOfBounds

Java 单选按钮alertDialog中的ArrayIndexOutOfBounds,java,android,radio-button,indexoutofboundsexception,Java,Android,Radio Button,Indexoutofboundsexception,我正在为调查服务开发一个应用程序。 用户必须输入其国家/地区才能注册。 我正在使用单选按钮的alertDialog 以下是我的java代码: public void onClickCountrySCI(View view) { final String[] CategorySelected = {null}; final String categories[] = {"USA", "Mexico", "Canada"}; AlertDialog.Builder bui

我正在为调查服务开发一个应用程序。 用户必须输入其国家/地区才能注册。 我正在使用单选按钮的alertDialog

以下是我的java代码:

public void onClickCountrySCI(View view) {

    final String[] CategorySelected = {null};
    final String categories[] = {"USA", "Mexico", "Canada"};

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Select Country:");
    builder.setSingleChoiceItems(categories, 0, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

            CategorySelected[0] = categories[i];
        }

    }).setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

            if (CategorySelected[0].equals("USA")) {

                et4.append(categories[i]);
            } else if (CategorySelected[1].equals("Mexico")) {

                et4.append(categories[i]);
            } else if (CategorySelected[2].equals("Canada")) {

                et4.append(categories[i]);
            }
        }

    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

            dialogInterface.dismiss();
        }
    }).create();
    builder.show();
}
我总是出错。是的

java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
我怎样才能摆脱这个? 提前谢谢

编辑 更新代码

这是我的更新代码

以下是我的EditText XML:

<EditText
    android:onClick="onClickCountrySCI"
    android:focusable="false"
    android:inputType="text"
    android:layout_marginTop="10dp"
    android:layout_marginStart="15dp"
    android:layout_below="@+id/et_fourteen_sci"
    android:id="@+id/et_fifteen_sci"
    android:hint="Country"
    android:textColor="@color/colorAccent"
    android:backgroundTint="@color/colorPrimary"
    android:layout_width="330dp"
    android:layout_height="45dp" />
这是我所有更新的代码

我不再收到错误,但是单选按钮值被放置到编辑文本中

尝试更改此选项:

builder.setSingleChoiceItems(categories, -1, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

            CategorySelected[0] = categories[i];
        }

您可以使用arrayList,而不是使用字符串数组,因为您之前没有指定列表的大小


而不是最终字符串[]CategorySelected={null};add ArrayListCategorySelected=新建ArrayList

编辑: 另外,如果不想更改代码并保留字符串数组,则必须更改此CategorySelected[0]=categories[i];to CategorySelected[i]=类别[i];因为每次添加到索引0时。
还要对数组进行初始化,如最后的字符串[]CategorySelected=newstring[4]

检查有多少数据。[n] ?这里是CategorySelected[]数组的问题。您已使用相同大小的类别[]进行初始化。e、 g.最终字符串[]CategorySelected={null,null,null};您不应该使用数组,而应该使用简单的字符串。将从数组中选择的类别转换为stringfinal String[]CategorySelected={null};初始化一些默认值您认为CategorySelected[1]的作用是什么?
builder.setSingleChoiceItems(categories, -1, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

            CategorySelected[0] = categories[i];
        }
final String categories[] = {"USA", "Mexico", "Canada"};

DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {

        //Add to arraylist here

        mCategorySelected.add(i,categories[i]); 
    }

}).setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {

        //check from arraylist here

        if (mCategorySelected.get(0).equals("USA")) {

            et4.append(categories[i]);
        } else if (mCategorySelected.get(1).equals("Mexico")) {

            et4.append(categories[i]);
        } else if (mCategorySelected.get(2).equals("Canada")) {

            et4.append(categories[i]);
        }
    }