Geocoder Context in Subclass
My Main Activity is AddressFinder
, here I start a AddressController
:
AddressController ac = new AddressController();
The AddressController should update in some cases:
import android.content.Context;
....
private void updateAddresses() throws IOException {
Geocoder geocoder = new Geocoder(context);
for (Address a: address) {
List<Address> addressIn = geocoder.getFromLocation(a.getLatitude(),
a.getLongitude(), 1);
}
}
Now I have no idea which context I have to use. I don't understand how to use it. I tried this
, context
, getBaseContext()
, getApp开发者_Go百科licationContext()
but nothing worked. Furthermore i tried to give the Adresscontroller an argument with the context
(getApplicationContext
) of the main activity.
I think i got a solution. In the main activity i use following:
ac.updateAddr(getApplicationContext());
In the AddressController i changed:
public void updateAddr(Context c) throws IOException {
updateAddresses(c);
}
Thats the way I give the context to the Geocoder
in the AddressController
. Works fine.
In your Activity
class create variable:
Context con = this;
...
Geocoder geocoder = new Geocoder(con);
Maybe use another constructor?:
public Geocoder (Context context, Locale locale)
精彩评论