title: MapView onTap issue: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
body: <p>There are <a href="https://stackoverflow.com/questions/7933206/android-unable-to-add-window-token-null-is-not-for-an-application-exception">similar questions</a> 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:</p>
  
  <p>You are trying to implement a customized ItemizedOverlay in a MapView, like this:</p>
  
  <pre><code>MyCustomItemizedOverlay mOverlay = new MyCustomItemizedOverlay(drawable, mContext);
  </code></pre>
  
  <p>Normally, if you are doing this from an activity, it is enough to use:</p>
  
  <pre><code>MyCustomItemizedOverlay mOverlay = new MyCustomItemizedOverlay(drawable, this);
  </code></pre>
  
  <p>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 <code>this</code> won't work. If you think (like I did) to change <code>this</code> to <code>getBaseContext()</code> or <code>getApplicationContext()</code>, then you will run into the following error when you try to tap on the ItemizedOverlay in your MapView:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>What is the cause of this error, and how do you fix it?</p>