Contextual issue in the Google Map View Android app

I tried to implement the Google Map View tutorial on the Android developer site, but I always have a problem when trying to display AlertDialog when I click on the overlay image. The problem is that mContext is null when called

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

in the HelloItemizedOverlay onTap method because the constructor

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

never gets called (what can I say) that initializes mContext. When i replace

 HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable); 

from

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

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

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

when I try to display AlertDialog as follows:

 dialog.show(); 

I assume that this lesson has been successfully implemented by thousands of people, so I'm at a loss why no one else has encountered this problem ... did I miss an important step from the textbook?

+4
source share
2 answers

I think you need to pass the mapView context to the HelloItemizedOverlay constructor, for example:

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

There is clearly an error in the sample codebook. Small errors and omissions like this are not uncommon in help documentation, especially for a large project such as Android.

+11
source

And in the constructor, it seems like it should be:

 super(boundCenterBottom(defaultMarker)); 

not

 super(defaultMarker); 
+1
source

Source: https://habr.com/ru/post/1301157/


All Articles