I'm new to Android. i'm trying to use this class I've downloaded.
public class BatchImageDownloader extends AsyncTask<Void, Void, Void> {
List<String> imgUrls = new ArrayList<String>();
BaseAdapter adapter;
HashMap<String, Drawable> imageCache;
Context ctxt;
public BatchImageDownloader(BaseAdapter adapter, HashMap<String, Drawable> imageCache, Context ctxt) {
this.adapter = adapter;
this.imageCache = imageCache;
this.ctxt = ctxt;
}
public void addUrl(String url) {
imgUrls.add(url);
Log.i("imgDownloader", "url ADDED!");
}
@Override
protected Void doInBackground(Void... params) {
for (String url : imgUrls) {
if (!imageCache.containsKey(url)) {
Drawable bm = downloadImage(url);
if (null != bm) {
imageCache.put(url, bm);
publishProgress();
}
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
adapter.notifyDataSetChanged();
}
@Override
protected void onPostExecute(Void result) {
adapter.notifyDataSetChanged();
}
public Drawable downloadImage(String url) {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
try {
HttpResponse response = httpClient.execute(request);
InputStream stream = response.getEntity().getContent();
Drawable drawable = Drawable.createFromStream(stream, "src");
//Log.i("imgDownloader", "image DOWNLOADED!");
//int duration = Toast.LENGTH_LONG;
//Toast.makeText(ctxt, "Inizio scaricamento", duration).show();
return drawable;
} catch (ClientProtocolException e) {
e.printStackTr开发者_开发百科ace();
return null;
} catch (IllegalStateException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (NullPointerException e){
e.printStackTrace();
return null;
}
}
}
Well, when I try to execute it by: batchDownloader.execute(); I get this:
05-13 16:13:22.750: ERROR/AndroidRuntime(29693): FATAL EXCEPTION: AsyncTask #1
05-13 16:13:22.750: ERROR/AndroidRuntime(29693): java.lang.RuntimeException: An error occured while executing doInBackground()
05-13 16:13:22.750: ERROR/AndroidRuntime(29693): at android.os.AsyncTask$3.done(AsyncTask.java:200)
05-13 16:13:22.750: ERROR/AndroidRuntime(29693): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
05-13 16:13:22.750: ERROR/AndroidRuntime(29693): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
05-13 16:13:22.750: ERROR/AndroidRuntime(29693): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
05-13 16:13:22.750: ERROR/AndroidRuntime(29693): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-13 16:13:22.750: ERROR/AndroidRuntime(29693): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
05-13 16:13:22.750: ERROR/AndroidRuntime(29693): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
05-13 16:13:22.750: ERROR/AndroidRuntime(29693): at java.lang.Thread.run(Thread.java:1096)
05-13 16:13:22.750: ERROR/AndroidRuntime(29693): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
05-13 16:13:22.750: ERROR/AndroidRuntime(29693): at android.os.Handler.<init>(Handler.java:121)
05-13 16:13:22.750: ERROR/AndroidRuntime(29693): at android.widget.Toast.<init>(Toast.java:68)
05-13 16:13:22.750: ERROR/AndroidRuntime(29693): at android.widget.Toast.makeText(Toast.java:231)
05-13 16:13:22.750: ERROR/AndroidRuntime(29693): at com.apps.EveryEyeFeedReader.BatchImageDownloader.downloadImage(BatchImageDownloader.java:73)
05-13 16:13:22.750: ERROR/AndroidRuntime(29693): at com.apps.EveryEyeFeedReader.BatchImageDownloader.doInBackground(BatchImageDownloader.java:43)
05-13 16:13:22.750: ERROR/AndroidRuntime(29693): at com.apps.EveryEyeFeedReader.BatchImageDownloader.doInBackground(BatchImageDownloader.java:1)
05-13 16:13:22.750: ERROR/AndroidRuntime(29693): at android.os.AsyncTask$2.call(AsyncTask.java:185)
05-13 16:13:22.750: ERROR/AndroidRuntime(29693): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-13 16:13:22.750: ERROR/AndroidRuntime(29693): ... 4 more
Anyone can help me? Thx
You can't interact with the UI in the doInBackground()
method, move your UI logic to the onPostExecute()
. That's why (presumably) you had to comment out the Toast
lines.
in your exception , there is something related to the Toast ,
note that anyThing which have relation with UI, the only thread who can touch it is the UIThread
so i think u should do something like this :
Ctx.runOnUIThread(new Runnable()
{@override
public void run(){ Toast.makeText(.............).show();
}
});
hope it helps :)
he means that , any instructions which has a relation with changing or modification of the UI Components, you should put it in the method onPostExecute()
so don't call for Example Toast.show() in your method doInBackground(...);
精彩评论