gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:56:36 26 4
gpt4 key购买 nike

我正在运行 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)

最佳答案

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

关于android - Temboo execute() 无法在未调用 Looper.prepare() 的线程内创建处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32109848/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com