4

I've tried to implement the Google Map View tutorial on the Android developer site, but I keep running into a problem when trying to display an AlertDialog when I click on the overlay image. The problem is that mContext is null when calling

AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);

in HelloItemizedOverlay's onTap method because the constructor

public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
    super(boundCenterBottom(defaultMarker));
    mContext = context;
}

is never called (that I can tell) which initializes mContext. When I replace

HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);

with

HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, 
                this.getApplicationContext());

in HelloGoogleMaps's onCreate method in order to initialize the context, I get an exception

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

when I try to display the AlertDialog like so:

dialog.show();

I assume this tutorial has been successfully implemented by thousands of people, so I'm at a loss why no one else has run into this problem... have I missed an important step from the tutorial?

tronman
  • 9,862
  • 10
  • 46
  • 61

2 Answers2

11

I think you need to pass mapView's context to the HelloItemizedOverlay constructor like so:

HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, mapView.getContext());

There is clearly an error in the tutorial's code sample. Small errors and omissions like this are not uncommon within reference documentation, especially for a large project like Android.

Jeff Gilfelt
  • 26,131
  • 7
  • 48
  • 47
  • "Small errors and omissions like this are not uncommon within reference documentation" - true, but it doesn't have to be this way. ;-) – ahsteele Jun 06 '10 at 22:06
  • [this question](http://stackoverflow.com/questions/3319158/googlemaps-mapview-crashes-ontap-in-android) addresses the same two mistakes in the tutorial. – galath Jul 16 '11 at 18:57
  • Because of my setup, ...(drawable, MyMapViewActivity.this.getParent()); worked for me. – worked Apr 08 '12 at 09:58
1

And in the constructor it seems should be:

super(boundCenterBottom(defaultMarker));

not:

super(defaultMarker);
everbird
  • 11
  • 1
  • 2