我有3个按钮和一个Gallery.In我的画廊我有3个ImageAdapters一个点击每个按钮,我正在设置一个缩略图图像URL,必须获取并加载到那些ImageAdapters,我正在使用AsyncTask对此。
代码:
class AddImageTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... unused) {
try {
for (String url : PhotoURLS) {
String filename = url.substring(url.lastIndexOf("/") +
1,url.length());
filename = "" + filename;
String thumburl = url.substring(0, url.lastIndexOf("/") + 1);
imageAdapter.addItem(LoadThumbnailFromURL(thumburl+ filename));
publishProgress();
}
for (String url : AudioThumbURLS) {
String filename = url.substring(url.lastIndexOf("/") +
1,url.length());
filename = "" + filename;
String thumburl = url.substring(0, url.lastIndexOf("/") + 1);
imageAdapter1.addItem(LoadThumbnailFromURL(thumburl+ filename));
publishProgress();
}
for (String url : VideoThumbURLS) {
String filename = url.substring(url.lastIndexOf("/") +
1,url.length());
filename = "" + filename;
String thumburl = url.substring(0, url.lastIndexOf("/") + 1);
imageAdapter2.addItem(LoadThumbnailFromURL(thumburl+ filename));
publishProgress();
}
} catch (Exception ae) {
ae.printStackTrace();
Toast.makeText(getApplicationContext(),"Error found in the application",
Toast.LENGTH_LONG).show();
}
return (null);
}
@Override
protected void onProgressUpdate(Void... unused) {
imageAdapter.notifyDataSetChanged();
imageAdapter1.notifyDataSetChanged();
imageAdapter2.notifyDataSetChanged();
}
@Override
protected void onPostExecute(Void unused) {
}
}
我的LoadThumbnailFromURL方法是:
private Drawable LoadThumbnailFromURL(String url) {
try {
URLConnection connection = new URL(url).openConnection();
String contentType = connection.getHeaderField("Content-Type");
boolean isImage = contentType.startsWith("");
if (isImage) {
HttpGet httpRequest = new HttpGet(url);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient
.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(
entity);
InputStream is = bufferedHttpEntity.getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} else {
Bitmap b = BitmapFactory.decodeResource(getResources(),
R.drawable.icon);
Drawable d = new BitmapDrawable(b);
return d;
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG)
.show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
return null;
}
应用程序启动正常,但在某段时间后抛出错误:
01-21 15:16:23.750:E / AndroidRuntime(8292):致命异常:AsyncTask#2 01-21 15:16:23.750:E / AndroidRuntime(8292):at java.util.concurrent.FutureTask $ Sync.innerSetException(FutureTask.java:273) 01-21 15:16:23.750:E / AndroidRuntime(8292):at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 01-21 15:16:23.750:E / AndroidRuntime(8292):at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:307) 01-21 15:16:23.750:E / AndroidRuntime(8292):at java.util.concurrent.FutureTask.run(FutureTask.java:137) 01-21 15:16:23.750:E / AndroidRuntime(8292):at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:305)
可能的原因是什么?如何解决?
答案 0 :(得分:3)
您是否有机会捕获异常,但在catch {}中获得另一个异常?由于它位于不同的(非UI)线程上,因此不要尝试使用Toast来通知自己错误。