【问题标题】:Android Index out of boundsAndroid 索引超出范围
【发布时间】:2023-10-26 12:01:01
【问题描述】:

我正在尝试将小时 scheldule 旁边的 bold 文本(来自http://golfnews.no/golfpaatv.php)放入字符串数组中,然后将其显示在我的设备屏幕上。我已经设置了 Internet 访问权限,所以这不是问题。应用程序在我的设备上崩溃了。原因:索引越界。我不明白问题出在哪里。我的代码是:

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


}

&lt;h2&gt;&lt;/h2&gt; 的数量低于 10 。我的应用程序在第二次迭代时崩溃。我是android初学者,所以不太了解。你能给我一个提示吗?我真的很感激 。谢谢!

PS:我知道 Index Out of bounds 是什么意思,我只是不知道为什么会出现这里的错误。

【问题讨论】:

  • 你说有一个IndexOutOfBoundsException;你能显示堆栈跟踪吗?

标签: android indexing webrequest bounds out


【解决方案1】:

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

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

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

在你的情况下,我怀疑trax 中有超过 10 个&lt;h2&gt;,所以你得到了这个例外。

一开始你不知道&lt;h2&gt;的数量所以改变这一行:

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

【讨论】:

  • +1 表示“检查堆栈跟踪以查找此异常源自的行”。
  • 我知道这一点,我不知道为什么在这个应用程序中,即使我写 'String result[] = new String[20]' ,我也会得到同样的错误。该页面的标题数量少于 10 个。
  • 的数量在 10 以下。我的应用程序在第二次迭代时崩溃。
  • 我已经进行了这些更改,但我的应用程序在同一(第二次)迭代中崩溃
  • 完成,trax = trax.substring(trax.indexOf("&lt;/h2&gt;") + 5);,现在,应用程序在第一次迭代时崩溃
【解决方案2】:

也试试这个..

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>"));

            }

【讨论】: