Android 添加折线到地图错误
Android Adding a polyline to map error
我的应用程序从 URL 检索 JSON
并解析从用户位置到所选标记位置的方向。
调试告诉我目的地和源是正确的,当我在地图中添加多段线时应用程序卡住了。这是代码:
for(int z = 0; z<list.size()-1;z++){
LatLng src= list.get(z);
LatLng dest= list.get(z+1);
PolylineOptions line= new PolylineOptions()
.add(src, dest)
.width(2)
.color(Color.BLUE).geodesic(true);
mMap.addPolyline(line);
}
这是编辑后的 logcat:
05-20 11:44:04.698 26190-27044/com.example.marco.progettoquinta E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
Process: com.example.marco.progettoquinta, PID: 26190
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.IllegalStateException: Not on the main thread
at maps.f.g.b(Unknown Source)
at maps.z.D.a(Unknown Source)
at maps.ag.t.a(Unknown Source)
at uz.onTransact(:com.google.android.gms.DynamiteModulesB:137)
at android.os.Binder.transact(Binder.java:387)
at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.addPolyline(Unknown Source)
at com.google.android.gms.maps.GoogleMap.addPolyline(Unknown Source)
at com.example.marco.progettoquinta.MapsActivity.drawPath(MapsActivity.java:210)
at com.example.marco.progettoquinta.JSONParser.getJSONFromUrl(JSONParser.java:80)
at com.example.marco.progettoquinta.JSONParser.doInBackground(JSONParser.java:35)
at com.example.marco.progettoquinta.JSONParser.doInBackground(JSONParser.java:18)
at android.os.AsyncTask.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
说出处不详,其实是对的。
当然这个方法是在AsyncTask.
中实现的
哪里出错了?
您的异常发生是因为您试图修改 UI 不是从主线程而是从异步任务的 doInBackground(这是您的请求所必需的)。
尝试将您的代码封装成这样的东西
runOnUiThread(new Runnable() {
@Override
public void run()
{
//do your loop adding polyline
}
});
或者获取从 doInBackground 返回的数据并处理它以在 OnPostExecute 中添加折线。
我的应用程序从 URL 检索 JSON
并解析从用户位置到所选标记位置的方向。
调试告诉我目的地和源是正确的,当我在地图中添加多段线时应用程序卡住了。这是代码:
for(int z = 0; z<list.size()-1;z++){
LatLng src= list.get(z);
LatLng dest= list.get(z+1);
PolylineOptions line= new PolylineOptions()
.add(src, dest)
.width(2)
.color(Color.BLUE).geodesic(true);
mMap.addPolyline(line);
}
这是编辑后的 logcat:
05-20 11:44:04.698 26190-27044/com.example.marco.progettoquinta E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
Process: com.example.marco.progettoquinta, PID: 26190
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.IllegalStateException: Not on the main thread
at maps.f.g.b(Unknown Source)
at maps.z.D.a(Unknown Source)
at maps.ag.t.a(Unknown Source)
at uz.onTransact(:com.google.android.gms.DynamiteModulesB:137)
at android.os.Binder.transact(Binder.java:387)
at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.addPolyline(Unknown Source)
at com.google.android.gms.maps.GoogleMap.addPolyline(Unknown Source)
at com.example.marco.progettoquinta.MapsActivity.drawPath(MapsActivity.java:210)
at com.example.marco.progettoquinta.JSONParser.getJSONFromUrl(JSONParser.java:80)
at com.example.marco.progettoquinta.JSONParser.doInBackground(JSONParser.java:35)
at com.example.marco.progettoquinta.JSONParser.doInBackground(JSONParser.java:18)
at android.os.AsyncTask.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
说出处不详,其实是对的。
当然这个方法是在AsyncTask.
中实现的
哪里出错了?
您的异常发生是因为您试图修改 UI 不是从主线程而是从异步任务的 doInBackground(这是您的请求所必需的)。 尝试将您的代码封装成这样的东西
runOnUiThread(new Runnable() {
@Override
public void run()
{
//do your loop adding polyline
}
});
或者获取从 doInBackground 返回的数据并处理它以在 OnPostExecute 中添加折线。