My first problem was when faithfully copying the code was in overriding the the boolean com.google.android.maps.ItemizedOverlay.onTap(int index) method to give a fatal exception.
FATAL EXCEPTION: main java.lang.NullPointerException at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:136) at android.app.AlertDialog$Builder.<init>(AlertDialog.java:353) at com.example.HelloItemizedOverlay.onTap(HelloItemizedOverlay.java:87)
Weird... what's going on here then. The first part of the tutorial tells me to delete the theme information so I thought that maybe I'd deleted a bit too much stuff or made another error... but no.
Of course the first thing I should have done is put in some breakpoints and step through to the offending line to look at my NullPointerException. This clearly told me that the mContext was null in HelloItemizedOverlay.onTap():
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
Well of course it was, I hadn't set it! Also, looking closely at the code I could see that the setting of the mContext variable was in the constructor that I didn't use because I hadn't changed this on the HelloGoogleMapsActivity class.
So I could see that the tutorial required more than just cutting and pasting so had a think about it and made the following changes:
1. HelloGoogleMapsActivity.onCreate() I changed the call to the overlay class HelloItemizedOverlay:
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);
Remember that Activity is an indirect subclass of Context so this is fine.
Activity extends android.view.ContextThemeWrapper which extends android.content.ContextWrapper which extends android.content.Context
Try using getApplicationContext() and you'll run into android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application errors.
2. HelloItemizedOverlay constructor chain to the other constructor instead of a call to super. The other constructor then calls super.
public HelloItemizedOverlay(Drawable defaultMarker, Context context) { this(defaultMarker); mContext = context; } public HelloItemizedOverlay(Drawable defaultMarker) { super(boundCenterBottom(defaultMarker)); }
That's it, done! The dialogue appears when the android is clicked:
No comments:
Post a Comment