0
votes

I ran into following error.

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

Where getErrorMessage() is Can't create handler inside thread that has not called Looper.prepare() and logUser is a function which just show toast congaing message.

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

2 Answers

2
votes

You cant do UI operations from background thread.

Try this:

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;
2
votes

You need to instantiate your AsyncTask on the main thread. The AsyncTask source code creates a Handler to call your onPreExecute(), onPostExecute(), etc. methods, and if that Handler is not instantiated on the main thread, Android will throw an exception telling you that the thread that Handler is interacting with has not had it's Looper.prepare() method called.