My Android Apps

Solbrent - UV Indeks. Sjekk yr.no sin UV prognose. Pass på deg og dine i Sola!



Badetemperaturer for Oslo. Local water temperatures for Oslo.


A New and fresh way to experience your Google Reader stream

Concerts and events from Sentrum scene, Rockefeller & John dee in Oslo

A specialist app for break baking enthusiasts. 

GPS Locate your phone from anywhere. Find your phone, by making it ring, when it is muted by sending it SMS. 

Følg meg på Twitter
My employer

Inmeta blogs
« Ny norsk Android app på market | Main | First encounter with the Amazon App store »
Tuesday
Jul192011

Unable to add window -- token null is not for an application

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)

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>