Temboo execute() Can't create handler inside thread that has not called Looper.prepare()

438 views Asked by At

I am running Temboo Google Distance Matrix. https://temboo.com/library/Library/Google/DistanceMatrix/DrivingDistanceMatrix/

So I run the InputSet in a separate AsyncTask, then in the onPostExecute method of that AsyncTask, I start another AsyncTask to run the ResultSet.

And the reason I didn't put them on 1 background task is because the .execute() method of the ResultSet opens another thread (I think), so I will have a concurrency exception there.

I also cannot just call .execute() in the onPostExecute() method because you cannot make network calls in the main thread.

However, my program keeps crashing at this line even though it's running on a separate thread now:

DrivingDistanceMatrixResultSet drivingDistanceMatrixResults = drivingDistanceMatrixChoreo.execute(drivingDistanceMatrixInputSet);

This is my code

public class BackgroundTembooInputs extends AsyncTask<String[], Void, Void> {
    private static DrivingDistanceMatrix drivingDistanceMatrixChoreo;
    private static DrivingDistanceMatrixInputSet drivingDistanceMatrixInputs;

    public BackgroundTembooInputs() {
    }

    @Override
    protected Void doInBackground(String[]... params) {
        TembooSession session = null;
        try {
            session = new TembooSession("accName", "appName", "appKey");

            drivingDistanceMatrixChoreo = new DrivingDistanceMatrix(session);

            drivingDistanceMatrixInputs = drivingDistanceMatrixChoreo.newInputSet();

            drivingDistanceMatrixInputs.set_Destinations("some addr"); 
            drivingDistanceMatrixInputs.set_Origins("some addr");

        } catch (TembooException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        BackgroundTembooOutputs backgroundTembooOutputs = new BackgroundTembooOutputs(mainWindowActivity);
        backgroundTembooOutputs.execute();
    }

    public static DrivingDistanceMatrix getDrivingDistanceMatrixChoreo() {
        return drivingDistanceMatrixChoreo;
    }

    public static DrivingDistanceMatrixInputSet getDrivingDistanceMatrixInputs() {
        return drivingDistanceMatrixInputs;
    }
}

public class BackgroundTembooOutputs extends AsyncTask<Void, Void, Void> {
    public BackgroundTembooOutputs(MainWindowActivity mainWindowActivity) {

    }

    @Override
    protected Void doInBackground(Void... params) {
        DrivingDistanceMatrix drivingDistanceMatrixChoreo = BackgroundTembooInputs.getDrivingDistanceMatrixChoreo();
        DrivingDistanceMatrixInputSet drivingDistanceMatrixInputSet = BackgroundTembooInputs.getDrivingDistanceMatrixInputs();

        try {
            // ERROR HERE
            DrivingDistanceMatrixResultSet drivingDistanceMatrixResults = drivingDistanceMatrixChoreo.execute(drivingDistanceMatrixInputSet);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}

And the Logcat:

08-20 01:35:32.163  19533-19550/edu.drexel.cs.ptn32.hw2 E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: prog, PID: 19533
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:304)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
            at android.os.Handler.<init>(Handler.java:200)
            at android.os.Handler.<init>(Handler.java:114)
            at android.widget.Toast$TN.<init>(Toast.java:344)
            at android.widget.Toast.<init>(Toast.java:100)
            at android.widget.Toast.makeText(Toast.java:258)
            at prog.BackgroundTembooOutputs.doInBackground(BackgroundTembooOutputs.java:35)
            at prog.BackgroundTembooOutputs.doInBackground(BackgroundTembooOutputs.java:17)
            at android.os.AsyncTask$2.call(AsyncTask.java:292)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
1

There are 1 answers

1
Anton Malyshev On BEST ANSWER

You cannot make Toasts in AsyncTask.doInBackground (since it is not running on UI thread) - move Toast creation to AsyncTask.onPreExecute or to AsyncTask.onPostExecute