I have encountered this problem twice now, So I can just as well put it up here.
If you get this error, or something like it, you are probably working on an Android app that uses the maps library. If I add an Overlay to a map by extending the ItemizedOverlay class, I must provide a constructor that keeps a reference to the Context.
public SomeItemizedOverlay(Drawable marker, Context context) {
super(boundCenterBottom(marker));
this.mContext = context;
populate();
}
When I implement the onTap method I give the context to a AlertDialog.Builder
@Override
protected boolean onTap(int index) {
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle("Title");
dialog.setMessage("Message");
dialog.show();
return true;
}
It is VERY importalt that the Context given to the AlertDialog.Builder is the Activity that extends MapActivity. If you do not, you end up with the exception below.
So, the code
SomeItemizedOverlay overlay =
new SomeItemizedOverlay (marker, getApplicationContext());
Does not work, but the following does
SomeItemizedOverlay overlay =
new SomeItemizedOverlay (marker, this);
ERROR/AndroidRuntime(29246): FATAL EXCEPTION: main
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRoot.setView(ViewRoot.java:562)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.app.Dialog.show(Dialog.java:265)
at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
at com.norsktrafikkinfo.activities.RoadMessagesItemizedOverlay.onTap(RoadMessagesItemizedOverlay.java:46)
at com.google.android.maps.ItemizedOverlay.onTap(ItemizedOverlay.java:453)
at com.google.android.maps.OverlayBundle.onTap(OverlayBundle.java:83)
at com.google.android.maps.MapView$1.onSingleTapUp(MapView.java:356)
at com.google.android.maps.GestureDetector.onTouchEvent(GestureDetector.java:533)
at com.google.android.maps.MapView.onTouchEvent(MapView.java:683)
at android.view.View.dispatchTouchEvent(View.java:3932)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:955)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1015)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1015)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1015)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1015)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1784)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1157)
at android.app.Activity.dispatchTouchEvent(Activity.java:2228)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1759)
at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2340)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1980)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4293)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
RROR/AndroidRuntime(29246): FATAL EXCEPTION: main android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application at android.view.ViewRoot.setView(ViewRoot.java:562) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) at android.app.Dialog.show(Dialog.java:265) at android.app.AlertDialog$Builder.show(AlertDialog.java:802) at com.norsktrafikkinfo.activities.RoadMessagesItemizedOverlay.onTap(RoadMessagesItemizedOverlay.java:46) at com.google.android.maps.ItemizedOverlay.onTap(ItemizedOverlay.java:453) at com.google.android.maps.OverlayBundle.onTap(OverlayBundle.java:83) at com.google.android.maps.MapView$1.onSingleTapUp(MapView.java:356) at com.google.android.maps.GestureDetector.onTouchEvent(GestureDetector.java:533) at com.google.android.maps.MapView.onTouchEvent(MapView.java:683) at android.view.View.dispatchTouchEvent(View.java:3932) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:955) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1015) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1015) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1015) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1015) at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1784) at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1157) at android.app.Activity.dispatchTouchEvent(Activity.java:2228) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1759) at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2340) at android.view.ViewRoot.handleMessage(ViewRoot.java:1980) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:143) at android.app.ActivityThread.main(ActivityThread.java:4293) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) at dalvik.system.NativeStart.main(Native Method)