Android索引越界



我试图得到粗体文本旁边的小时时间表(从http://golfnews.no/golfpaatv.php)把它放在一个字符串数组,然后,显示在我的设备的屏幕上。我已经设置了互联网接入许可,所以这不是问题。应用程序在我的设备上崩溃了。原因:索引越界。我不明白问题出在哪里。我的代码是:

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;
    }

}

<h2></h2>的个数小于10。我的应用程序在第二次迭代时崩溃了。我是android的初学者,所以我知道的不多。你能给我个提示吗?我真的很感激。谢谢!

PS:我知道Index Out of bounds是什么意思,我只是不知道为什么我在这里得到错误

当您想要访问超出范围的数组索引时,您将获得IndexOutOfBoundsException。例如:

String[] myArray = new String[10];
myArray[10] = "test"; // 10 is out of limits(0-9)

将产生这样的异常。检查此异常起源于的那一行的堆栈跟踪。它告诉你这个问题来自的类名/方法名/行号。

在您的情况下,我怀疑trax中有超过10个<h2>,因此您得到此异常。

最初你不知道<h2>的数量,所以改变这一行:

String result[] = new String[10];
与这个:

ArrayList<String> result= new ArrayList<String>(); 

然后你可以用下面的命令向列表中添加元素:

// result[i] = trax.substring(s1+4,s2);
result.add(trax.substring(s1+4,s2));

EDIT1
我也认为你是这个意思:

//trax = trax.substring(trax.indexOf("</h2>"));
trax = trax.substring(s2 + 5);

EDIT2

我也意识到你分配数组错误,你分配10个字符的字符串,而不是10个字符串的数组:

//String aux[] = new String[10];
String[] aux = new String[10];
//String result[] = new String[10];
String[] result = new String[10];

试试这个。

if(trax.indexOf("<h2>")!=-1&&i<10)
            {

            ok=1;
            s1 =trax.indexOf("<h2>");
            s2 = trax.indexOf("</h2>");
            result[i] = trax.substring(s1+4,s2);
            i++;
            trax = trax.substring(trax.indexOf("</h2>"));
            }

最新更新