Thursday, July 08, 2010

MapView Google Development Tutorial Context Bugs

There are some bugs in the code that Google supplies to demonstrate the Google Maps API.

The original Google Tutorial is here:
http://developer.android.com/resources/tutorials/views/hello-mapview.html

There is a useful Google Groups post regarding the major bug / confusion with this tutorial:
http://groups.google.com/group/android-beginners/browse_thread/thread/9d175c71b8b6825d

The differences are shown below

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);

List mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.dot_bar);
DefaultItemizedOverlay itemizedoverlay = new DefaultItemizedOverlay(drawable,this);

GeoPoint point = new GeoPoint(19240000,-99120000);
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");

itemizedoverlay.addOverlay(overlayitem);
itemizedoverlay.ReadyOverlay();
mapOverlays.add(itemizedoverlay);


}
}


Changing the code in onCreate to supply 'this' as a Context for the ItemizedOverLay is critical, otherwise you will get a NullReferenceException on the mContext field when onTap(int) attempts to create a Alert Dialog.

1 comment:

Anonymous said...

this was also posted on some forums about 'droids