[英]Android: fill spinner with items in text file
I want fill spinner with items in text file on my hosting. 我希望填充微调器在我的主机上的文本文件中的项目。 The text file content separate with "\\n" character: 文本文件内容以“\\ n”字符分隔:
Afghanistan
Albania
Algeria
Andorra
Angola
I write this code but I've this error : 我写这段代码,但我有这个错误:
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++;
}
you need to change the condition.. with this: 你需要改变条件..用这个:
while (line[index] != null) {
str.add(line[index]);
index++;
}
if say the size of line is 10, index++ will make it 11. thus line[11] will not be null but rather throw an ArrayIndexOutOfBoundsException
. 如果说行的大小是10,则index ++将使它成为11.因此,行[11]不会为null,而是抛出一个ArrayIndexOutOfBoundsException
。
change the condition to : 将条件更改为:
for(int i=0;i<line.length;i++) {
if(line[i] != null) {
str.add(line[index]);
}
}
You need to be check by the length of your String array in for loop 您需要在for循环中检查String数组的长度
for(int i=0;i<line.length;i++) {
if(line[i] != null) {
str.add(line[index]);
}
}
Change code to: 将代码更改为:
bo.write(buffer);
String s = bo.toString();
final Vector<String> str = new Vector<String>();
String[] line = s.split("\n");
int index = 0;
while (index < line.length) {
str.add(line[index]);
index++;
}
You are getting java.lang.ArrayIndexOutOfBoundsException
because you are not doing bound checking for line array. 您将获得java.lang.ArrayIndexOutOfBoundsException
因为您没有对行数组进行绑定检查。
It should be: 它应该是:
int index = 0, length = line.length;
while (index < length) {
str.add(line[index++]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.