Temboo execute() 无法在未调用 Looper.prepare() 的线程内创建处理程序
Temboo execute() Can't create handler inside thread that has not called Looper.prepare()
我是运行ning Temboo Google距离矩阵。
https://temboo.com/library/Library/Google/DistanceMatrix/DrivingDistanceMatrix/
所以我 运行 InputSet 在一个单独的 AsyncTask 中,然后在那个 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.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.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.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.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)
您不能在 AsyncTask.doInBackground 中创建 Toast(因为它不是 运行 在 UI 线程上)- 将 Toast 创建移动到 AsyncTask.onPreExecute 或 AsyncTask.onPostExecute
我是运行ning Temboo Google距离矩阵。 https://temboo.com/library/Library/Google/DistanceMatrix/DrivingDistanceMatrix/
所以我 运行 InputSet 在一个单独的 AsyncTask 中,然后在那个 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.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.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.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.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)
您不能在 AsyncTask.doInBackground 中创建 Toast(因为它不是 运行 在 UI 线程上)- 将 Toast 创建移动到 AsyncTask.onPreExecute 或 AsyncTask.onPostExecute