我想用我的主机上的文本文件中的项目填充微调器。 文本文件内容以“\n”字符分隔:
Afghanistan
Albania
Algeria
Andorra
Angola
我写了这段代码,但出现了这个错误:
03-10 12:32:07.716: E/AndroidRuntime(7670):
FATAL EXCEPTION: Thread-69056
03-10 12:32:07.716:
E/AndroidRuntime(7670):
java.lang.ArrayIndexOutOfBoundsException:
length=91; index=91
bo.write(buffer);
String s = bo.toString();
final Vector<String> str = new Vector<String>();
String[] line = s.split("\n");
int index = 0;
while (line[index] != null) {
str.add(line[index]);
index++;
}
最佳答案
你需要改变条件.. 与此:
while (line[index] != null) {
str.add(line[index]);
index++;
}
如果说行的大小是 10,index++ 会将其变为 11。
因此 line[11] 不会为空,而是抛出 ArrayIndexOutOfBoundsException
.
将条件更改为:
for(int i=0;i<line.length;i++) {
if(line[i] != null) {
str.add(line[index]);
}
}
关于java - Android:用文本文件中的项目填充微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22299024/