出现此错误:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at java.lang.Thread.run(Thread.java:1027)
Caused by: java.lang.NullPointerException
at com.Wahoo.BrowseListActivity$DownloadSite.doInBackground(BrowseListActivity.java:79)
at com.Wahoo.BrowseListActivity$DownloadSite.doInBackground(BrowseListActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
... 4 more
这是我的AsyncTask ...它在特定时间仅针对某些用户崩溃...不确定原因。也许他们在查询中丢失了他们的互联网连接?我在这做错了什么?这是我的代码:
private class DownloadSite extends AsyncTask<String, Integer, String> {
private HttpResponse response;
private InputStream in;
private Context context;
private String html;
private ProgressDialog progress;
@Override
protected String doInBackground(String... params) {
in = null;
String url = "aURLGOESHERE_BUTI'MCENSORING" + params[0] + "";
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = null;
try {
response = client.execute(request);
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
in = response.getEntity().getContent();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
html = null;
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(in));
} catch (Exception e) {
this.publishProgress();
this.cancel(true);
e.printStackTrace();
}
StringBuilder str = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
str.append(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
html = params[0] + str.toString();
return html;
}
@Override
protected void onPreExecute() {
progress = new ProgressDialog(BrowseListActivity.this);
progress.setIndeterminate(true);
progress.setMessage("Loading...");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.show();
}
@Override
protected void onProgressUpdate(Integer... progress) {
CharSequence text = "Connection interrupted...please try again";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(getApplicationContext(), text,
duration);
toast.show();
}
@Override
protected void onPostExecute(String html) {
progress.dismiss();
Context context = BrowseListActivity.this;
Intent stopViewer = new Intent(context, StopActivity.class);
stopViewer.setData(Uri.parse(html + ""));
context.startActivity(stopViewer);
}
}
答案 0 :(得分:3)
你做错的一件事是在错误之后继续执行doInBackground
,这使得无法继续有意义地继续。例如:
try {
response = client.execute(request);
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
如果这引发异常,response
将成为null
,并且没有必要继续前进。您将在下一个代码块中生成NullPointerException
。这不会是致命的,因为你正在捕捉那里的所有例外。不过,这种模式会重复,而且你没有抓住所有例外情况。
您应该提前退出,返回null
作为String结果。然后,您可以在null
中测试onPostExecute
,并让用户以优雅的方式了解所发生的事情。