问题描述
- 无法在线程中创建handler
-
错误:logUser("An error happend while creating graph:"+ getErrorMessage());
getErrorMessage()
无法在线程中创建handler,线程没有调用Looper.prepare()
,而logUser 是用来显示toast信息的函数。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(); }
解决方案
同上求解,请问你解决了吗?
解决方案二:
需要在主线程中实例化 AsyncTask 。AsyncTask 源代码创建一个handler
来调用你的onPreExecute()
,onPostExecute()
等方法。如果这个handler没有在主线程实例化。Android系统就会抛出异常,告诉你线程和Handler的互动缺少 Looper.prepare()
方法的调用。
时间: 2024-12-30 23:28:29