2012-02-03 69 views
1

我試圖在我的應用程序中實現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

回答

1

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