循环中的AsyncTask?

问题描述:

我试图在我的应用程序中实现AsyncTask。问题是它是从不同的线程创建和执行的,所以我得到了异常。我向前移动并实现了可创建并执行我的AsyncTask的小型runnable。我在runOnUIThread()方法运行此运行的,但仍然得到了我可运行的构造这个错误,对符合AsyncTask构造:循环中的AsyncTask?

Can't create handler inside thread that has not called `Looper.prepare()` 

任何想法怎么办?

需要编码?

myLocationOverlay.runOnFirstFix(new Runnable() { 

      @Override 
      public void run() { 
       fillMap(); 
      } 
     }); 

public void fillMap(){ 
      runOnUiThread(new AsyncTaskRunner()); 
} 

private class AsyncTaskRunner implements Runnable { 
     DownloadDataTask task; 

     public AsyncTaskRunner(double latitude, double longitude, int radius) { 
      super(); 

      this.task = new DownloadDataTask(latitude, longitude, radius); 
     } 

     @Override 
     public void run() { 
      task.execute(); 
     } 
    } 
+0

什么是DownloadDataTask? – aromero 2012-02-03 13:27:34

+0

DownloadDataTask是我的类,它扩展了AsyncTask类 – Seraphis 2012-02-03 13:28:51

+0

为什么你只是没有调用Looper.prepare()方法? – 2012-02-03 13:30:29

AsyncTask的构造函数仍在非UI线程上调用。你可以移动AsyncTask的构造来运行方法吗?

private class AsyncTaskRunner implements Runnable { 
    double latitude; 
    double longitude; 
    int radius; 

    public AsyncTaskRunner(double latitude, double longitude, int radius) { 
     super(); 
     this.latitude = latitude; 
     this.longitude = longitude; 
     this.radius = radius; 
    } 

    @Override 
    public void run() { 
     new DownloadDataTask(latitude, longitude, radius).execute(); 
    } 
} 
+0

嘿,傻我..这是如此明显..谢谢! – Seraphis 2012-02-03 13:40:20