2012-03-18 77 views
0

以下是我想要首先执行的操作。我试图启动一个MapView,它抓取用户的位置并加载一个覆盖图,其中的数据根据​​用户的位置而改变。从处理程序创建的aSyncTask

代码概述: 我有一个MapView,它抓住当前用户在创建时的位置。 的locationOverlay我使用召唤出一个Runnable上首次定位:

locationOverlay.runOnFirstFix(centerAroundFix); 

这Runnable接口,centerAroundFix,启动一个的AsyncTask。只要我称之为“新的AsyncTask”,这个错误被抛出:

Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 

的的AsyncTask的工作是获取数据,创建覆盖,并添加覆盖。创建并执行asynctask可以在Runnable之外正常工作。

回答

0

不知道我怎么没有看到这已经......

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

我敢肯定这是一个重复的问题。

编辑:作品。这是我的代码。用“mHandler.post”包装updateLocation()调用:

private Handler mHandler = new Handler(Looper.getMainLooper()); 

private Runnable centerAroundFix = new Runnable() { 
    public void run() { 
     /* Jump to current location and set zoom accordingly */ 
     map.getController().animateTo(locationOverlay.getMyLocation()); 
     map.getController().setZoom(12); 
     /* This is a different thread. I need to call this from a thread that has a 'Looper' prepared. */ 
     mHandler.post(new Runnable() { 
      public void run() { 
       updateLocation(); 
      } 
     }); 
    } 
}; 
0

您必须在UIThread中创建Handler。例如,作为班级成员:

public class Activity extends Activity{ 
    private Handler handler = new Handler();   
} 
相关问题