Well I can work out the problem - but not a pleasant solution.
Your activity indirectly subclasses ContextWrapper
, which overrides getPackageName
. The Geocoder
creates a GeocoderParams
with the given context (this
in your case) and that in turn asks the context for its package name.
Unfortunately, ContextWrapper.getPackageName
is implemented by calling getPackageName
on the context that it's wrapping - which is null in your case. As far as I can tell, That's why you're getting the NullPointerException
.
You could override getPackageName
yourself (in your activity). That would remove that particular exception, but it feels like an ugly solution. This looks like something that would always be a problem using this approach - is it the recommended approach in tutorials?
Looking at some other sample code, I think you may want to just change this:
Geocoder coder = new Geocoder(this);
to:
private final Geocoder coder = new Geocoder(getApplicationContext());
(The private final
part is just a matter of good practice, and isn't part of the fix here.)
EDIT: It now looks like getApplicationContext()
is failing for the same reason. It's possible (I'm not an Android dev) that anything context-specific is only available after the application has started. You might want to use this instead:
private Geocoder coder;
@Override public void onStart() {
coder = new Geocoder(this);
}
Basically, you can only create a Geocoder
when you've got a properly initialized context - so that's the thing to investigate.