First, sorry for my bad english, I'm portuguese speaker and surely I will make some english grammar mistake.
Well, I'm trying to do a AsyncTask Class to load a map from googleMap, but it's showing this error message:
06-25 18:47:03.062: E/AndroidRuntime(8021): FATAL EXCEPTION: AsyncTask #1
06-25 18:47:03.062: E/AndroidRuntime(8021): java.lang.RuntimeException: An error occured while executing doInBackground()
06-25 18:47:03.062: E/AndroidRuntime(8021): at java.lang.Thread.run(Thread.java:856)
06-25 18:47:03.062: E/AndroidRuntime(8021): Caused by: java.lang.IllegalStateException: Not on the main thread
My main class what calls the AsyncTask class
public class ExemploGPSSimples extends FragmentActivity {
protected GoogleMap map;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.exmeplo_gps);
}
@Override
protected void onResume() {
super.onResume();
configuraGPS();
}
private void configuraGPS() {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.fragment_gps);
map = mapFragment.getMap();
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
//LatLng local=pegaPosicaoAtual();
Localizador localizador = new Localizador(this);
String endereco="Av. João Pessoa, 1091, Porto Alegre, RS";
LatLng local = localizador.getEndereco(endereco);
SincronizaMapa sincronizaMapa = new SincronizaMapa(this,local,map);
sincronizaMapa.execute();
adicionaMarcador(map,local);
map.setMyLocationEnabled(true);
}
private void adicionaMarcador(GoogleMap map2, LatLng local) {
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(local).title("Luiz").snippet("Casa");
Marker marker = map2.addMarker(markerOptions);
}
}
and finally the AsyncTask class
public class SincronizaMapa extends AsyncTask<Void, Void, Void> {
private final Activity activity;
private final LatLng local;
private final GoogleMap map;
private ProgressDialog progressDialog;
public SincronizaMapa(Activity activity, LatLng local,
GoogleMap map) {
this.activity = activity;
this.local = local;
this.map = map;
}
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(activity, "Sincronizando mapa", "Aguarde...",true,true);
}
@Override
protected Void doInBackground(Void... params) {
CameraPosition position = new CameraPosition.Builder()
.target(local)
.bearing(0)
.tilt(0)
.zoom(17)
.build();
CameraUpdate update = CameraUpdateFactory.newCameraPosition(position);
map.animateCamera(update);
return null;
}
@Override
protected void onPostExecute(Void result) {
progressDialog.dismiss();
}
}
Thank you all for helping.
An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread.
To start an AsyncTask the following snippet must be present in the MainActivity class : MyTask myTask = new MyTask(); myTask. execute(); In the above snippet we've used a sample classname that extends AsyncTask and execute method is used to start the background thread.
Rules of AsyncTask: This class must be loaded on the UI thread. This is done automatically as from JELLY_BEAN. 2. The task instance must be created on the UI thread.
Why Android AsyncTask is Deprecated? Here is the official reason it is deprecated. AsyncTask was intended to enable proper and easy use of the UI thread. However, the most common use case was for integrating into UI, and that would cause Context leaks, missed callbacks, or crashes on configuration changes.
map.animate modifies a UI component (the map), so that must be done on the UI thread. You can move code that needs to be on the UI thread to onPostExecute
The problem is that you are trying to update the UI from a thread other than the UI thread. asyncTasks allow you to do background work (doInBackgroud) while keeping the UI thread free and then update the UI accodingly (onPostExecute).
You should put the body of doInBackground in the body of onPostExecute - the method that runs on the UI thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With