java.lang.Integer类型的source [0]无法存储在java.lang.CharSequence []类型的目标数组中

时间:2015-05-18 14:38:05

标签: android

我尝试在onNewIntent方法的onPostexecute方法中获取意图后,显示AlertBuild对话框。 routeList有[1,9,13]。目前我收到此错误java.lang.ArrayStoreException: source[0] of type java.lang.Integer cannot be stored in destination array of type java.lang.CharSequence[]

ArrayList<Integer> routeList = extras
                .getIntegerArrayList("stop_route"); //[1,9,13]
        int routeListSize = routeList.size();
        if(routeListSize > 0){

        CharSequence charSequence[] = routeList
                .toArray(new CharSequence[routeList.size()]);
        for (int i = 0, size = routeList.size(); i < size; i++) {
            charSequence[i] = String.valueOf(routeList.get(i));
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle("Select");

                builder.setMultiChoiceItems(charSequence, null,
            new DialogInterface.OnMultiChoiceClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which,
                        boolean isChecked) {
                    // TODO Auto-generated method stub

                }
            });
     }

2 个答案:

答案 0 :(得分:6)

尝试像这样更改for

String[] charSequence = new String[routeList.size()];
        for (int i = 0, i<routeList.size(); i++) {
            charSequence[i] = String.valueOf(routeList.get(i));
        }

而不是:

CharSequence charSequence[] = routeList
                .toArray(new CharSequence[routeList.size()]);
        for (int i = 0, size = routeList.size(); i < size; i++) {
            charSequence[i] = String.valueOf(routeList.get(i));
        }

答案 1 :(得分:0)

CharSequence只能使用字符串值,但是您的routeList类型是Integer。因此,当您用数据填充charSequence[i]时,需要将项目类型更改为String

错误:charSequence[i] = routeList.get(i);

右:charSequence[i] = routeList.get(i).toString();