Android Looper.prepare() 和 AsyncTask

发布于 2025-01-04 06:52:41 字数 1693 浏览 1 评论 0原文

您好,我有一些关于 Looper.prepare() 和 AsyncTasks 的问题。

在我的应用程序中,我有一个正在启动其他 AsyncTask 的 AsyncTask。我有 2 个 AsyncTasks Search 和 GetImage。 GetImage 任务在搜索任务中执行多次。效果很好。

然而最近我实现了图像缓存,如下所述: http://android-developers.blogspot.com/2010/07 /multithreading-for-performance.html

实现此功能后,我开始间歇性崩溃

02-09 17:40:43.334: W/System.err(25652): java.lang.RuntimeException: 无法在未调用 Looper.prepare() 的线程内创建处理程序

我不确定应该在哪里调用prepare()。这是代码的粗略轮廓

Search extends AsyncTask{

    @Override
    protected void doInBackground(){
        ArrayList<Object> objs = getDataFromServer();
        ArrayList<View> views = new ArrayList<View>();
        for(Object o: objs){
            //multiple AsyncTasks may be started while creating views
            views.add(createView(o));
        }
        for(View v: views){
            publishProgess(v);
        }
   }
}

public View createView(Object o){

    //create a view
    ImageView iv = .....;
    ImageDownloader.getInstance().download(url,iv);
}

ImageDownloader 可以在上面的链接中看到,其中还有另一个用于下载图像的 AsyncTask。它还包含一个 Handler 和 Runnable,用于清除每次下载时都会重置的缓存。我确实对 ImageDownloader 进行了一项更改,将其设为单例。

 public static ImageDownloader getInstance(){
    if(instance == null){
        //tried adding it here but it results in occasional
        //cannot create more than one looper per thread error
        Looper.prepare();
        instance= new ImageDownloader();
    }
    return instance;
}

ImageDownloader 下载方法可能会被调用 10 次,这会为每次下载创建一个 AysncTask。这几天一直在摸索,希望大家能帮忙。

Hi I have some questions regarding Looper.prepare() and AsyncTasks.

In my app I have an AsyncTask that's starting other AsyncTasks. I had 2 AsyncTasks Search and GetImage. The GetImage task is executed several times within the Search task. It works fine.

However recently I implemented image caching as described here:
http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html

After I implemented this I started getting intermittent crashes

02-09 17:40:43.334: W/System.err(25652): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

I'm not sure where I should call prepare(). Here's a rough outline of the code

Search extends AsyncTask{

    @Override
    protected void doInBackground(){
        ArrayList<Object> objs = getDataFromServer();
        ArrayList<View> views = new ArrayList<View>();
        for(Object o: objs){
            //multiple AsyncTasks may be started while creating views
            views.add(createView(o));
        }
        for(View v: views){
            publishProgess(v);
        }
   }
}

public View createView(Object o){

    //create a view
    ImageView iv = .....;
    ImageDownloader.getInstance().download(url,iv);
}

The ImageDownloader can be seen in the link above, in it is another AsyncTask to download the image aswell. It also contains a Handler and Runnable for purging the cache which is reset every time a download occurs. I did make one change to the ImageDownloader, I made it a singleton.

 public static ImageDownloader getInstance(){
    if(instance == null){
        //tried adding it here but it results in occasional
        //cannot create more than one looper per thread error
        Looper.prepare();
        instance= new ImageDownloader();
    }
    return instance;
}

The ImageDownloader download method may be called 10's of times, which is creating an AysncTask for each of the downloads. So I've been scratching my head for the last few days, Hope you guys can help.

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

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

发布评论

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

评论(1

且行且努力 2025-01-11 06:52:41

真正发生的事情是您尝试在需要 UI 线程运行的后台线程上执行某些操作。

Looper 是系统的一部分,确保事务按顺序完成,并且设备按其应有的方式响应。

95% 的情况下,当您收到 Looper 错误时,它的真正含义是您需要将部分代码移至 UI 线程,在 Asynctask 中,这意味着将其移至 onPostExecuteonProgressUpdate

在您的情况下,看起来好像您正在添加视图,这是用户界面的一部分,因此会导致问题。如果这实际上不是导致问题的原因,那么检查堆栈跟踪应该会给您一些线索。

作为旁注,如果您必须调用 Looper.prepare(),我会在线程的开头调用它。但是,通常建议这样做以避免调用它。

What is really going on is you are attempting to perform something on a background thread that requires the UI thread to run.

The Looper is a part of the system that ensures that transactions are done in order, and the device is responding as it should.

95% of the time when you get the Looper error, what it really means is you need to move part of your code to the UI thread, in Asynctask this means moving it to either onPostExecute or onProgressUpdate.

In your case it appears as if you are adding views, which is part of the UI and therefor would cause a problem. If that is not in fact what is causing the problem, an examination of the stacktrace should give you some clues.

As a side note, if you MUST call Looper.prepare() I would call it at the beginning of your thread. However, this is generally recommended to avoid the need to call it.

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