我一直在解析 html 以获得特定链接的标题。我可以将此字符串添加到字符串数组中,但我想从驻留在我的 strings.xml 资源中的字符串数组中添加一个字符串。
int i = 1;
String time = null;
for (Element title : rels) {
time = getResources().getStringArray(R.array.times)[i];
titleArray.add(time + title.attr("title"));
i++;
}
这是我的字符串资源:
<string-array name="times">
<item name="1">12:00 am</item>
<item name="2">12:30 am</item>
<item name="3">1:00 am</item>
<item name="4">1:30 am</item>
<item name="5">2:00 am</item>
</string-array>
我在 LogCat 中收到此错误:
04-06 01:44:08.837: E/AndroidRuntime(30938): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=48; index=48
我在这个字符串数组中有 48 个字符串,这与我从 html 中解析的字符串数量完全相同。我不确定为什么我的应用程序会强制关闭。有人知道吗?
最佳答案
数组索引从 0 而不是 1 开始。将 i
更改为从 0
开始:
int i = 0;
关于java - 从 strings.xml 添加字符串到 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22890879/