私はAndroidが初めてです。 私がダウンロードしたこのクラスを使用しようとしています。ERROR/AndroidRuntime(29693):原因:java.lang.RuntimeException:Looper.prepare()を呼び出していないスレッド内でハンドラを作成できません。
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.printStackTrace();
return null;
} catch (IllegalStateException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (NullPointerException e){
e.printStackTrace();
return null;
}
}
}
よく、私が実行しようとすると:batchDownloader.execute();私はこれを得る:
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
誰でも手伝ってもらえますか? Thx
「あなたのUIロジックをonPostExecute()に移動する」と言ったらどうしましたか? どの命令が間違った場所にありますか? このnoobの質問に申し訳ありません.. – Nemus