3

There are similar questions to this, but it may not be obvious where the problem lies to someone (like me) trying to implement onTap events for a MapView . The problem/question is this:

You are trying to implement a customized ItemizedOverlay in a MapView, like this:

MyCustomItemizedOverlay mOverlay = new MyCustomItemizedOverlay(drawable, mContext);

Normally, if you are doing this from an activity, it is enough to use:

MyCustomItemizedOverlay mOverlay = new MyCustomItemizedOverlay(drawable, this);

which is a line you can find in various tutorials (including Google's HelloItemizedOverlay example), and you may have copied it from one of them. If, however, you try to move this code anywhere other then the activity's main thread (for example, into an AyncTask), then this won't work. If you think (like I did) to change this to getBaseContext() or getApplicationContext(), then you will run into the following error when you try to tap on the ItemizedOverlay in your MapView:

08-23 09:59:21.444: E/AndroidRuntime(30056): FATAL EXCEPTION: main
08-23 09:59:21.444: E/AndroidRuntime(30056): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
08-23 09:59:21.444: E/AndroidRuntime(30056):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:589)
08-23 09:59:21.444: E/AndroidRuntime(30056):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
08-23 09:59:21.444: E/AndroidRuntime(30056):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
08-23 09:59:21.444: E/AndroidRuntime(30056):    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
08-23 09:59:21.444: E/AndroidRuntime(30056):    at android.app.Dialog.show(Dialog.java:277)
08-23 09:59:21.444: E/AndroidRuntime(30056):    at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
08-23 09:59:21.444: E/AndroidRuntime(30056):    at com.myapp.MyCustomItemizedOverlay.onTap(MyCustomItemizedOverlay.java:32)

What is the cause of this error, and how do you fix it?

Community
  • 1
  • 1
robguinness
  • 16,266
  • 14
  • 55
  • 65

2 Answers2

3

The problem lies in the fact that you need to specify the Activity context where the ItemizedOverlay will be displayed, NOT the Application context. This is not immediately clear from the error message, and I believe the source of this confusion can be found in an error of Google's MapView Tutorial. It explicitly says to reference the application context, when you should be referencing the activity context where the ItemizedOverlay will be displayed. Thus, you can solve this problem by changing the referenced line above to:

MyCustomItemizedOverlay mOverlay = new MyCustomItemizedOverlay(drawable, MyActivity.this);

or even better:

MyCustomItemizedOverlay mOverlay = new MyCustomItemizedOverlay(drawable, mapView.getContext());

where mapView is the instance of the MapView where the ItemizedOverlay will actually be displayed.

robguinness
  • 16,266
  • 14
  • 55
  • 65
1

had a problem creating alert dialog...solved by changing 'getApplicationContext()'to 'MyActivity.this'....

thanks to @robguinness :D

Nigel Crasto
  • 318
  • 3
  • 8