AsyncTask and Contexts

Posted by Michael on Stack Overflow See other posts from Stack Overflow or by Michael
Published on 2009-12-16T06:31:43Z Indexed on 2010/03/30 9:43 UTC
Read the original article Hit count: 457

So I'm working out my first multi-threaded application using Android with the AsyncTask class. I'm trying to use it to fire off a Geocoder in a second thread, then update the UI with onPostExecute, but I keep running into an issue with the proper Context.

I kind of hobbled my way through using Contexts on the main thread, but I'm not exactly sure what the Context is or how to use it on background threads, and I haven't found any good examples on it. Any help? Here is an excerpt of what I'm trying to do:

public class GeoCode extends AsyncTask<GeoThread, Void, GeoThread> {
  @Override
  protected GeoThread doInBackground(GeoThread... i) {
    List<Address> addresses = null;
    Geocoder geoCode = null; 
    geoCode = new Geocoder(null); //Expects at minimum Geocoder(Context context);
    addresses = geoCode.getFromLocation(GoldenHour.lat, GoldenHour.lng, 1);
  }
}

It keeps failing at the sixth line there, because of the improper Context.

© Stack Overflow or respective owner

Related posts about android

Related posts about java