0

我是 Java 新手,但我之前在 C 中做过一些 PSP 编码。这似乎完全不同。

下面是我所看到的堆栈跟踪:

java.lang.RuntimeException: An error occured while executing doInBackground()
enter code hereenter code here`at android.os.AsyncTask$3.done(AsyncTask.java:299)
enter code here`at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at com.adev.abmp3.ViewSongDialog$FindLyrics.doInBackground(ViewSongDialog.java:173)
at com.adev.abmp3.ViewSongDialog$FindLyrics.doInBackground(ViewSongDialog.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

这是代码补丁:

@Override
protected Void doInBackground(Void... arg0) {
    String searchQuery = title.replaceAll("[^ \\w]", "");
    searchQuery = searchQuery.replace(" ", "+");
    String searchResult = httpRun("http://search.azlyrics.com/search.php?q="+searchQuery).toString();
    System.out.println("http://search.azlyrics.com/search.php?q="+searchQuery);
    if(!searchResult.contains("Sorry, no results") && !searchResult.equals("")) {
        String link = searchResult.split("<table width=100%>")[1];
        link = link.substring(link.indexOf("<a href=\""), link.indexOf("\" rel"));
        link = link.replace("<a href=\"", "");

        String lR = httpRun(link).toString();
        if(lR != "" && lR.contains("<!-- start of lyrics -->")) {
            lR = lR.substring(lR.indexOf("<!-- start of lyrics -->"), lR.indexOf("<!-- end of lyrics -->"));
            lR = lR.replace("<!-- start of lyrics -->", "");
            lyrics = lR;
        } else {
            fail = 1;
            lyrics = "Couldn't find lyrics";
        }
    } else {
        fail = 1;
        lyrics = "Couldn't find lyrics";
    }
    return null;
}

我做了一些研究,并读到你不能在里面打印到屏幕上doInBackground——但我不确定从这里去哪里。任何帮助,将不胜感激。

编辑:java.lang.String.startEndAndLength 中的 java.lang.StringIndexOutOfBoundsException

继承人的新堆栈:

java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.StringIndexOutOfBoundsException: length=7338; regionStart=537; regionLength=-538
at java.lang.String.startEndAndLength(String.java:593)
at java.lang.String.substring(String.java:1474)
at com.adev.abmp3.ViewSongDialog$FindLyrics.doInBackground(ViewSongDialog.java:174)
at com.adev.abmp3.ViewSongDialog$FindLyrics.doInBackground(ViewSongDialog.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
4

2 回答 2

3

您的 split 调用返回一个大小为 1 的数组,但您正试图获取其中的第二个项目(使用 [1])。您必须考虑您的拆分字符串不在输入中的可能性。

于 2013-02-26T20:56:40.977 回答
0
String link = searchResult.split("<table width=100%>")[1];

错误正在那里发生。索引从 0 开始,并且您的拆分返回一个大小为 1 的数组,因此元素 1 超出范围,因此引发异常。如果将其更改为String link = searchResult.split("<table width=100%>")[0];将不再抛出异常。

于 2013-02-26T21:12:43.067 回答