我一直得到ArrayIndexOutOfBoundsException

时间:2013-09-01 10:40:35

标签: android

在尝试显示特定项目时继续获取ArrayIndexOutOfBoundsException数组列表数据在logcat中显示正常但在显示抛出异常之后请帮助是android的新代码

public void loadData(){
        InputStream file = ourContext.getResources().openRawResource(R.raw.cashpot);
        BufferedReader reader = new BufferedReader(new InputStreamReader(file));       
        String line ="";
        ArrayList<String> values = new ArrayList<String>();
        try {
            while((line = reader.readLine()) != null) { 
                values.add(line);
              //  String[] strings = line.split(",");


                /*for(String s : strings) {
                    values.add(s);
                }*/

            }
            reader.close();
            //for(String s : values) Log.d("CSV Test", s);
            for(int i = 0; i < values.size();i++){
                String[] data = values.get(i).split(",");
                Log.d("Test String",data[2] );

            }
        } catch (IOException e) {
            e.printStackTrace();
        }



    }

2 个答案:

答案 0 :(得分:0)

你的某些行没有逗号(,)(或1个逗号),你试图用逗号分隔它们并将它存储到数据数组中。并且您正在访问数据[2]位置。可能没有创建,这就是你得到这个例外的原因。

答案 1 :(得分:0)

您的数组中可能没有第3个元素,即在“,”分割后创建的第3个元素。 您可以在访问数据[2]之前打印数据值(可能不存在)。 或者,您可以调试程序并逐步进行并监视每个变量的值。