我正在运行 Temboo Google 距离矩阵。 https://temboo.com/library/Library/Google/DistanceMatrix/DrivingDistanceMatrix/
所以我在一个单独的 AsyncTask 中运行 InputSet,然后在该 AsyncTask 的 onPostExecute 方法中,我启动另一个 AsyncTask 来运行 ResultSet。
我没有将它们放在 1 个后台任务上的原因是因为 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)