Bernardi Marco Bernardi Marco - 4 months ago 58x
Android Question

Android Adding a polyline to map error

My application retrieve a

from URL and parse the directions from the user's location to the selected marker's location.
The debug shows me that the destination and the source are correct, by the way the app gets stuck when I add the polyline in the map. Here is the code:

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)

And here is the edited 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$3.done(
at java.util.concurrent.FutureTask.finishCompletion(
at java.util.concurrent.FutureTask.setException(
at android.os.AsyncTask$SerialExecutor$
at java.util.concurrent.ThreadPoolExecutor.runWorker(
at java.util.concurrent.ThreadPoolExecutor$
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 Source)
at uz.onTransact(
at android.os.Binder.transact(
at$zza$zza.addPolyline(Unknown Source)
at Source)
at com.example.marco.progettoquinta.MapsActivity.drawPath(
at com.example.marco.progettoquinta.JSONParser.getJSONFromUrl(
at com.example.marco.progettoquinta.JSONParser.doInBackground(
at com.example.marco.progettoquinta.JSONParser.doInBackground(
at android.os.AsyncTask$
at android.os.AsyncTask$SerialExecutor$ 
at java.util.concurrent.ThreadPoolExecutor.runWorker( 
at java.util.concurrent.ThreadPoolExecutor$ 

It says that the source is unknown but it's actually correct.
Of course this method is implemented in a

Where is the error?


Your exception occurs because you're trying to modify the UI not from the main thread but from the doInBackground of your asynctask (which is required for your request). Try encapsulating your code into something like this

runOnUiThread(new Runnable() {

   public void run()
      //do your loop adding polyline

or get your data returned from your doInBackground and process it to add the polylines in the OnPostExecute.