2013-05-05 77 views
0

我遇到以下错误。无法在未调用Looper.prepare()的线程内创建处理程序Graphhopper

logUser("An error happend while creating graph:"+ getErrorMessage()); 

凡getErrorMessage()是无法创建内螺纹已不叫Looper.prepare()和LOGUSER是只显示敬酒congaing消息的函数的处理程序。

void prepareGraph() { 
    logUser("loading graph (" + Helper.VERSION + "|" + Helper.VERSION_FILE 
      + ") ... "); 
    new MyAsyncTask<Void, Void, Path>() { 
     protected Path saveDoInBackground(Void... v) throws Exception { 
      GraphHopper tmpHopp = new GraphHopper().forAndroid(); 
      tmpHopp.contractionHierarchies(true); 
      tmpHopp.load(mapsFolder + currentArea); 
      logUser("found graph with " + tmpHopp.getGraph().nodes() + " nodes"); 
      hopper = tmpHopp; 
      return null; 
     } 

     protected void onPostExecute(Path o) { 
      if (hasError()) { 
       logUser("An error happend while creating graph:" 
         + getErrorMessage()); 
      } else { 
       logUser("Finished loading graph. Touch to route."); 
       calcPath(52.534185, 13.348732, 52.53857, 
         13.41259); 
      } 

      finishPrepare(); 
     } 
    }.execute(); 
} 

回答

2

你不能从后台线程做UI操作。

试试这个:

GraphHopper tmpHopp = new GraphHopper().forAndroid(); 
tmpHopp.contractionHierarchies(true); 
tmpHopp.load(mapsFolder + currentArea); 
runOnUiThread(new Runnable() {  
    public void run() { 
     logUser("found graph with " + tmpHopp.getGraph().nodes() + " nodes"); 
    } 
}); 
hopper = tmpHopp; 
return null; 
+0

非常感谢您的帮助。 – 2013-05-05 19:35:50

+0

如果问题得到解决,您可以将此答案标记为已接受 – 2013-05-05 19:40:06

2

您需要在主线程中实例化AsyncTask。该AsyncTask源代码创建一个Handler打电话给你onPreExecute()onPostExecute()等方法,如果这Handler没有在主线程实例化,Android将抛出一个异常,告诉您该线程Handler与一直没有它的Looper.prepare()互动方法称为。

相关问题