ERROR/AndroidRuntime(29693):由以下原因引起:java.lang.RuntimeException:无法在未调用 Looper.prepare() 的线程内创建处理程序

发布于 2024-11-07 05:52:17 字数 4519 浏览 0 评论 0原文

我是安卓新手。 我正在尝试使用我下载的这个课程。

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

有人可以帮助我吗? 谢谢

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.printStackTrace();
        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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ペ泪落弦音 2024-11-14 05:52:17

您无法在 doInBackground() 方法中与 UI 交互,请将 UI 逻辑移至 onPostExecute() 中。这就是为什么(大概)您必须注释掉 Toast 行。

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.

情释 2024-11-14 05:52:17

在你的例外中,有一些与Toast相关的东西,

请注意,任何与UI有关的东西,唯一可以接触它的线程是UIThread,

所以我认为你应该做这样的事情:

Ctx.runOnUIThread(new Runnable()
{@override
public void run(){ Toast.makeText(.............).show();
}
});

希望它有帮助:)

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 :)

浅唱々樱花落 2024-11-14 05:52:17

他的意思是,任何与更改或修改 UI 组件有关的指令,您应该将其放在方法 onPostExecute() 中,

因此不要在方法 doInBackground(...) 中调用 Example Toast.show() ;

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(...);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文