gpt4 book ai didi

java - Android - java.lang.ArrayIndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-12-01 23:59:28 24 4
gpt4 key购买 nike

我是 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.StringIndexOutOfBoundsException在java.lang.String.startEndAndLength中

这是新堆栈:

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)

最佳答案

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

关于java - Android - java.lang.ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15099069/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com