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?