应用程序在尝试访问阵列Android SDK时崩溃
这是我试图在线程中解释的文件应用程序在尝试访问阵列Android SDK时崩溃,android,arrays,list,Android,Arrays,List,这是我试图在线程中解释的文件 % $- background:#000000 % Hello, World. It is currently a test of the features of YeML. % $- background:#ffffff % $+ id:test type:container 代码如下: new Thread() { StringBuilder text = new StringBuilder(); @Override
% $- background:#000000 % Hello, World. It is currently a test of the features of YeML. % $- background:#ffffff % $+ id:test type:container
代码如下:
new Thread() {
StringBuilder text = new StringBuilder();
@Override
public void run() {
try
{
String str = "";
URL url = new URL("http://example.com/" + mes + "/" + mes + ".meb");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
while ((str = in.readLine()) != null) {
text.append(str);
}
in.close();
} catch (MalformedURLException e1)
{
}
catch (IOException e)
{
}
if(message.contains(".meb")) {
String str[] = text.toString().split("%");
for (final String l : str) {
String code[] = l.split(" ");
if (l.toString().contains("$-")) {
for(String p : code)
{
if(p.toString().contains("background"))
{
final String set[] = p.toString().split(":");
runOnUiThread(new Runnable() {
@Override
public void run() {
(findViewById(R.id.body)).setBackgroundColor(Color.parseColor(set[1]));
}
});
}
}
}
else if(l.toString().contains("$+"))
{
String[] g = code[1].split(":");
String[] c = code[2].split(":");
final String[] test = {g[1], "100"};
globvar.add(test);
if(test[1].toString().equals("container")) {
runOnUiThread(new Runnable() {
@Override
public void run() {
final LinearLayout element = new LinearLayout(getApplicationContext());
element.setId(Integer.parseInt(test[1]));
element.setBackgroundColor(Color.parseColor("#000000"));
((LinearLayout) findViewById(R.id.linearo)).addView(element);
}
});
}
}
else
{
runOnUiThread(new Runnable() {
@Override
public void run() {
final TextView tv = new TextView(getApplicationContext());
tv.setTextColor(Color.parseColor("#000000"));
tv.setText(l);
((LinearLayout) findViewById(R.id.linearo)).addView(tv);
}
});
}
}
}
else {
if(message == null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
}
}
}
}.start();
我知道它发生在final String[]test={g[1],“100”}部分;因为这里是日志:
08-01 17:05:36. 186 20045 20065 E AndroidRuntime: FATAL EXCEPTION: Thread-1111
08-01 17:05:36. 186 20045 20065 E AndroidRuntime: java.lang.ArrayIndexOutOfBoundsException: length=1 ; index=1
08-01 17:05:36. 186 20045 20065 E AndroidRuntime: at example.yemeb.List$1.run(List.java:86)
前面给出的代码行在第86行。我不知道为什么会出现数组异常,因为字符串数组应该包含以下内容:
id:测试->id测试
应该有一个索引为0(id)的数组和一个索引为1(test)的数组。有什么问题?谢谢你的帮助。我想问题是它说数组长度是1,索引是1。。但指数应该是0?java索引从0开始
尝试在调试器中检查数组长度或输出一个logcat。logcat在这里非常清楚。它告诉您您正试图访问
g[1]
,但g[]``只有一个元素。因此,您只有g[0]
在您的代码中,您从不访问任何
[0]
索引。也许您认为java中的数组是以元素1
开始的在java中,数组从元素0
开始,我认为您应该首先检查g的长度,即使您说过字符串是id:testI,我想我发现了问题。当它显示%$+id:test-type:container时,由于字符串的设置方式,数组可能正在添加一个空白数组。我发现了这个问题并解决了它。感谢您的回复。:)我解释过了。它应该在那之后得到下一个元素。