2013-05-05 132 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()互動方法稱爲。

相關問題