Android Looper.prepare()和AsyncTask

tri*_*ggs 2 multithreading android handler android-asynctask

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

在我的应用程序中,我有一个启动其他AsyncTasks的AsyncTask.我有2个AsyncTasks搜索和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);
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

ImageDownloader下载方法可以被调用10次,这为每个下载创建了一个AysncTask.所以过去几天我一直在摸不着头脑,希望你们能帮忙.

Ash*_*thi 6

真正发生的是你试图在需要UI线程运行的后台线程上执行某些操作.

Looper是系统的一部分,可确保按顺序完成事务,并且设备正在响应.

95%的情况下,当你得到Looper错误时,它真正意味着你需要将部分代码移动到UI线程,在Asynctask中这意味着将它移动到onPostExecute或者onProgressUpdate.

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

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