Android的索引出界 [英] Android Index out of bounds
问题描述
我想从 HTTP GET旁边小时scheldule(即粗体文本: //golfnews.no/golfpaatv.php )把它放在一个String数组,然后,拿出来我的设备的屏幕上。我已经把互联网访问权限,所以这是不是我的设备上problem.The应用程序崩溃。原因:索引越界。我不明白的地方就是问题所在。我的code是:
I am trying to get the bold text next to the hour scheldule ( from http://golfnews.no/golfpaatv.php )put it in a String array , then , show it on my device's screen. I've put the Internet Access permission , so that is not the problem.The application crashes on my device . Reason : index out of bounds . I don't understand where's the problem . My code is :
package com.work.webrequest;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class WebRequest extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String trax;
String aux[] = new String[10];
setContentView(R.layout.main);
TextView txt = (TextView) findViewById(R.id.textView1);
trax=getPage();
aux= title (trax);
txt.setText(" Here I will display every title!!!");
}
private String[] title (String trax)
{
String result[] = new String[10];
int ok=1;
int s1,s2;
int i=0;
while(ok==1)
{
System.out.println("INDEX = "+trax.indexOf("<h2>"));
ok=0;
if(trax.indexOf("<h2>")!=-1)
{
ok=1;
s1 =trax.indexOf("<h2>");
s2 = trax.indexOf("</h2>");
result[i] = trax.substring(s1+4,s2);
i++;
trax = trax.substring(trax.indexOf("</h2>"));
}
}
return result;
}
private String getPage() {
String str = "***";
try
{
HttpClient hc = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.golfnews.no/feed.php?feed=1");
HttpResponse rp = hc.execute(post);
if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
str = EntityUtils.toString(rp.getEntity());
}
}catch(IOException e){
e.printStackTrace();
}
return str;
}
}
1所述的数量; H2&GT;
和&LT; / H&GT;
低于10。我的应用程序崩溃在第二迭代。我在Android上初学者,所以我不知道多少。你能不能给我一个提示?我真的AP preciate它。谢谢!
The number of <h2>
and </h2>
is below 10 . My application crashes at the 2nd iteration . I am a beginner in android , so I don't know much . Could you give me a hint ? I would really appreciate it . Thank you !
PS:我知道什么索引超出界限的手段,我只是不知道为什么我在这里得到的错误
推荐答案
您获得 IndexOutOfBoundsException异常
当你要访问的是超出范围的数组索引。例如:
You get IndexOutOfBoundsException
when you want to access an array index which is out of range. For example:
String[] myArray = new String[10];
myArray[10] = "test"; // 10 is out of limits(0-9)
会产生这样的异常。检查是否有此异常起源来自该行的堆栈跟踪。它告诉你的类名/方法名/线,这个问题是来自数。
Would produce such an exception. Check the stacktrace for the line that this exception originates from. It tells you the class name/method name/line number that this problem comes from.
在你的情况,我怀疑有超过10 &LT; H2&GT;
在 TRAX
等你拿此异常。
In your case I suspect there are more than 10 <h2>
in the trax
, so you get this exception.
最初,你不知道的数量&LT; H2&GT;
是如此改变这一行:
Initially you don't know the number of <h2>
's so change this line:
String result[] = new String[10];
本:
ArrayList<String> result= new ArrayList<String>();
然后你可以用下面的元素添加到这个列表:
Then you can add elements to this list with the following:
// result[i] = trax.substring(s1+4,s2);
result.add(trax.substring(s1+4,s2));
EDIT1 结果
此外,我想你的意思是这样的:
EDIT1
Also I think you mean this:
//trax = trax.substring(trax.indexOf("</h2>"));
trax = trax.substring(s2 + 5);
EDIT2
此外,我意识到你分配阵列错了,你是分配的10个字符,而不是10个字符串数组的字符串:
Also I realized you allocate arrays wrong, you are allocating a String of 10 chars instead of an array of 10 Strings:
//String aux[] = new String[10];
String[] aux = new String[10];
//String result[] = new String[10];
String[] result = new String[10];
这篇关于Android的索引出界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!