Temboo execute()无法在未调用Looper.prepare()的线程内创建处理程序

时间:2015-08-20 04:58:55

标签: android android-asynctask temboo

我正在运行Temboo Google Distance Matrix。 https://temboo.com/library/Library/Google/DistanceMatrix/DrivingDistanceMatrix/

所以我在一个单独的AsyncTask中运行InputSet,然后在该AsyncTask的onPostExecute方法中,我启动另一个AsyncTask来运行ResultSet。

我没有将它们放在一个后台任务上的原因是因为ResultSet的.execute()方法打开另一个线程(我认为),所以我会在那里遇到并发异常。

我也不能在onPostExecute()方法中调用.execute(),因为你不能在主线程中进行网络调用。

然而,即使它现在在一个单独的线程上运行,我的程序仍然在这条线上崩溃:

DrivingDistanceMatrixResultSet drivingDistanceMatrixResults = drivingDistanceMatrixChoreo.execute(drivingDistanceMatrixInputSet);

这是我的代码

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;
    }
}

和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 个答案:

答案 0 :(得分:1)

你不能在AsyncTask.doInBackground中制作Toast(因为它没有在UI线程上运行) - 将Toast创建移动到AsyncTask.onPreExecute或AsyncTask.onPostExecute