从Android中的文本文件中读取数据什么都不返回?

时间:2012-02-25 18:37:10

标签: android file text file-io

这是我的第一个问题,但我会尽力提问。无论如何,我正在尝试从Android中的文本文件中读取数据,但由于某种原因它不会返回任何内容 - 即IndexOutOfBounds异常。所有相关代码:

public Question getNextQuestion(int num) {
    Log.d(TAG, array.toString());
    return array.get(num+1);
}
public ArrayList<Question> getData(String str) throws NumberFormatException, IOException {
    Log.d(TAG, "STARTING getData");
    Log.d(TAG, str);
    ArrayList<Question> a = new ArrayList<Question>();
    String[] strline = str.split("//");
    for (int j = 0; j < strline.length; j++) {
        if (strline[j] == null) {
            strline[j] = "TESTING";
            Log.d(TAG, strline[j]);
        }
    }

    int num = 0;
    int x = 0;
    String y = strline[x];
    while (x == 0 || y != null) {
        num++;
        String[] data = y.split("//");
        Log.d(TAG, y);
        if (data.length >= 7) {
            a.add(new Question(Integer.parseInt(data[0]), data[1], data[2], data[3], data[4], data[5], Integer.parseInt(data[6])));
            Log.d(TAG, a.get(a.size()).getText());
        }
        else if (data.length >= 3) {
            a.add(new Question(Integer.parseInt(data[0]), data[1], data[2]));
            Log.d(TAG, a.get(a.size()).getText());
        }
        x++;
        if (x < strline.length)
            y = strline[x];
        else
            break;
    }
    for (int i=0; i<a.size(); i++){
        Log.d(TAG, a.get(i).getText());
    }
    Log.d(TAG, "ENDING getdata");
    return a;
}
public ArrayList<Question> openFile() throws IOException {
    Log.d(TAG, "STARTING openFile()");
    //FileReader fr = new FileReader("data/data/scibowl.prog/questionsb.txt");
    FileInputStream fis;
    String storedString = "";
    try {
        fis = openFileInput("questionsb.txt");
        DataInputStream dataIO = new DataInputStream(fis);
        String strLine = null;

        while ((strLine = dataIO.readLine()) != null) {
            storedString = storedString + strLine;
        }

        dataIO.close();
        fis.close();
    }
    catch  (Exception e) {  
    }

    array = getData(storedString);
    Log.d(TAG, "DID open file, ending openFile()");
    return array;
}

这将在LogCat中打印:

    onCreate done
STARTING openFile()
STARTING getData
ENDING getdata
DID open file, ending openFile()
right before Toast.makeText
[]
Caused by: java.lang.IndexOutOfBoundsException: Invalid location 1, size is 0
at java.util.ArrayList.get(ArrayList.java:341)
at scibowl.prog.Round.getNextQuestion(Round.java:55)
at scibowl.prog.RoundView.<init>(RoundView.java:26)
at scibowl.prog.Round.onCreate(Round.java:35)

我几乎尝试过从StringBuffers到FileInputStreams到BufferedReaders的所有内容,并阅读相关的Android文档,但请随时告诉我,我的阅读不够。有没有人知道为什么我不断获得没有元素的数组?

文本文件供参考,尽管经过轻微编辑以符合blockquote规则:

  

1 //在标准电磁频谱上,哪个频率在伽马射线和紫外线之间?//无线电波//微波// X射线//红外波// 1

     

2 //电磁波谱中的哪些波大致包含10 ^ 9 Hz到10 ^ 12 Hz的频率?//可见光//伽马射线//紫外线//微波// 3

     

3 //美国大约有多少百分比来自煤炭?// 30%// 40%// 50%// 60%// 0

     

4 //如果一个100公斤的人从静止开始走路并在2秒内以恒定的加速度加速到4.0米/秒,他做了多少工作?// 600 J // 800 J // 1200 J / / 2000 J // 1

1 个答案:

答案 0 :(得分:1)

您的logcat建议它在方法getNextQuestion的第55行崩溃,我相信是:

return array.get(num+1);

此外,您的logcat建议您尝试访问不存在的数组中的值。这意味着你试图在索引1处检索项目(我相信第二项),但是数组的大小为0,因此没有可以获得的项目。

很有可能在getData中你没有像你想要的那样实际返回ArrayList。或者您返回的ArrayList为空。