嗨我有AsyncTask的问题,即时解析来自网络的一些数据,但是当我想使用这个类时,我的应用程序崩溃了:
10-31 12:29:29.941: E/AndroidRuntime(3741): FATAL EXCEPTION: AsyncTask #2
10-31 12:29:29.941: E/AndroidRuntime(3741): java.lang.RuntimeException: An error occured while executing doInBackground()
10-31 12:29:29.941: E/AndroidRuntime(3741): at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-31 12:29:29.941: E/AndroidRuntime(3741): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-31 12:29:29.941: E/AndroidRuntime(3741): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-31 12:29:29.941: E/AndroidRuntime(3741): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-31 12:29:29.941: E/AndroidRuntime(3741): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-31 12:29:29.941: E/AndroidRuntime(3741): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-31 12:29:29.941: E/AndroidRuntime(3741): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-31 12:29:29.941: E/AndroidRuntime(3741): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-31 12:29:29.941: E/AndroidRuntime(3741): at java.lang.Thread.run(Thread.java:856)
10-31 12:29:29.941: E/AndroidRuntime(3741): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
10-31 12:29:29.941: E/AndroidRuntime(3741): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
10-31 12:29:29.941: E/AndroidRuntime(3741): at java.util.ArrayList.get(ArrayList.java:304)
10-31 12:29:29.941: E/AndroidRuntime(3741): at morisson.Notowaniagieldowe.MainActivity$Cena.doInBackground(MainActivity.java:169)
10-31 12:29:29.941: E/AndroidRuntime(3741): at morisson.Notowaniagieldowe.MainActivity$Cena.doInBackground(MainActivity.java:1)
10-31 12:29:29.941: E/AndroidRuntime(3741): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-31 12:29:29.941: E/AndroidRuntime(3741): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-31 12:29:29.941: E/AndroidRuntime(3741): ... 5 more
,代码是:
public class Cena extends AsyncTask<Integer,Void,String>{
ProgressDialog dialog;
@Override
protected String doInBackground(Integer... params) {
int a = params[0];
try {
Document doc = Jsoup.connect("http://www.money.pl/gielda/gpw/akcje/").get();
Elements elements = doc.select("td.ar");
System.out.println("test2:" + elements.get(a).text());
return elements.get(a).text();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
我还使用了另外两个使用AsyncTask的类但我对它们没有任何问题。当我试图打印“test2”时出现崩溃,当我试图返回一个值时出现崩溃。有什么建议吗?
答案 0 :(得分:0)
如果仔细检查错误日志,您会发现此处的实际错误是ArrayIndexOutOfBound
异常。含义elements.get(a)
或int a = params[0];
可能会返回null。
由于未捕获异常(导致崩溃),我强烈怀疑int a = params[0];
是导致问题的原因,因为它是唯一不在try catch
块中的代码。
答案 1 :(得分:0)
try {
Document doc = Joup.connect("http://www.money.pl/gielda/gpw/akcje/").get();
Elements elements = doc.select("td.ar");
if(elemetns!=null&&a!=null){
System.out.println("test2:" + elements.get(a).text());
return elements.get(a).text();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}