46 Responses

  1. kailuo wang
    kailuo wang at |

    It worked for me. Thanks!

  2. Deepu
    Deepu at |

    thanks man… good work 🙂

  3. JayCal
    JayCal at |

    Excellent writeup! Very clear and easy to understand.

  4. Animesh
    Animesh at |

    Your post helped me a lot! Thanks for sharing this tip 🙂

  5. Joe Li
    Joe Li at |

    This is very useful, thank you for the exp!

  6. C L
    C L at |

    Your post worked for me as well. (Wish they’d fix the Creating Dialogs Dev Guide.) Thank you.

  7. Gary
    Gary at |

    I was messing around for an hour with this one before your solution worked, many thanks from Ireland

  8. Dan Breslau
    Dan Breslau at |

    Another word of thanks. Your explanation about using getApplicationContext vs. “this” makes sense, but it definitely isn’t obvious. I’m glad I hit google immediately when I saw the error while creating a dialog.

  9. FluteDogs
    FluteDogs at |

    Very helpfuly explanation, men. Thank’s, this is the key of all our problems.

  10. Jack
    Jack at |

    Brilliant! You’re a Lifesaver.

  11. Lope Emano
    Lope Emano at |

    It really helps having stuff like this on the net. Google’s own dev site sucks. just try the mapactivity tut. geez.

  12. Narla
    Narla at |

    Too Good!!! Fixed my issue!! Thanks a bunch…

  13. Sam
    Sam at |

    In my case, I needed the context for an AlertDialog.Builder() within an OnItemClickListener, in which case I couldn’t just use “this”. When I attempted to use getApplicationContext() I got the BadTokenException error.

    After finding the source of the error from this article, I
    1 – Created a variable in the class scope: “private Context thisContext;”
    2 – Defined it in the onCreate method of the activity: “thisContext = this;”
    3 – And used it in my OnItemClickListener as follows: “alertDialog = new AlertDialog.Builder(thisContext).create();”

  14. sunit
    sunit at |

    thanks man, it is working for me.

  15. Ashok Domadiya
    Ashok Domadiya at |

    i used ActivityGroup in my apps.
    then setContentView(LayoutInflater.from(getParent()).inflate(R.layout.fuel,null));

    when i click on DateSetDialog button error generate like
    04-11 13:44:31.560: ERROR/AndroidRuntime(22212): android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an application

    solve it plz
    thanks

  16. Matthew hutchings
    Matthew hutchings at |

    Read a few of your posts and have thought they were really intresting. looking forward to seeing more from you.

  17. Liam
    Liam at |

    Cheers, worked for me.

  18. sunil
    sunil at |

    Good Idea ,it really works ,

  19. Venkat
    Venkat at |

    Thanks ! It worked for me too!

  20. manoj
    manoj at |

    cool man it worked for me also..thanks a lot.

  21. bobo
    bobo at |

    You are the MAN! 🙂 Tnx

  22. dodo
    dodo at |

    Thanks a lot!

    Strange that Android/Google guys used buggy code in their tutorials.

  23. raman
    raman at |

    very much useful, thanks a lot:)

  24. Margot
    Margot at |

    Doesn’t seem to work. When I try what you say I get an error on that line saying, “type mismatch: cannot convert from ExpandableListCalender.MyExpandableListAdapter to Context”

  25. Margot
    Margot at |

    Okay… sort of figured out the problem. Well… I do not understand completly why what I changed worked as I am very new to Android but you can disregard my last post as it now works. I guess I just have to figure out why but since I am knew to programming as a whole I am guessing it is something obvious. Anyhow, thanks.

  26. Hongge
    Hongge at |

    I used “XXXActivity.this” everywhere needed the object of Context,but it occured this problem sometimes yet!
    Sometimes it’s ok but sometimes it shows “Uable to add Window” exception.
    I just don’t know why

  27. android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an application - 关注安卓(Android)

    […] 这是解释http://tech.shantanugoel.com/2010/07/08/badtokenexception-android-dialog-getapplicationcontext.html。 […]

  28. Vinod
    Vinod at |

    Thanks mate.. my issue is fixed.

  29. Vinod
    Vinod at |

    I need some help on this..

    I need to read an image file from the sdcard and create another image(same content with different resolution) image on the sdcard or the res folder.

    I tried using FileInputStream to read the image contents, but once i use…..

    fileIn = openFileInput(Environment.getExternalStorageDirectory().toString()+”/ab.jpg”);

    its throwin exception.

    Can anyone suggest me on this….
    Do anyone have a piece of code that can read and create image on sdcard.

    thank u.

  30. Lalit
    Lalit at |

    Thanks its work for me…

  31. Dipti Desai
    Dipti Desai at |

    Thanx buddy..It worked

  32. Nonickh
    Nonickh at |

    It works, thank you very much

  33. Patrick
    Patrick at |

    Much thx .. worked .. saved me lot of work

  34. 18thAngel
    18thAngel at |

    THX A LOT

  35. Romain
    Romain at |

    Really usefull ! Thanks a lot

  36. steak
    steak at |

    Thanks a lot!

  37. yu
    yu at |

    owesome.Thx

  38. Xics
    Xics at |

    Great you solve my problem , thanks a lot

  39. android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an applic - 移动端设计与开发 - 开发者第1748897个问答

    […] 后来找到了一篇文章,地址是 http://tech.shantanugoel.com/2010/07/08/badtokenexception-android-dialog-getapplicationcontext.html 关于错误的解释是这样的: Solution: Just replace “getApplicationContext()” with “this” (i.e. “Context mContext = this;” ) and it will work fine. 解决方法:使用this替换掉getApplicationContext()即可正常使用。 […]

  40. android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an applic - 移动端开发 - 开发者

    […] 后来找到了一篇文章,地址是 http://tech.shantanugoel.com/2010/07/08/badtokenexception-android-dialog-getapplicationcontext.html 关于错误的解释是这样的: Solution: Just replace “getApplicationContext()” with “this” (i.e. “Context mContext = this;” ) and it will work fine. 解决方法:使用this替换掉getApplicationContext()即可正常使用。 […]

  41. android.view.WindowManager$BadTokenException:Unabletoaddwindow - 移动开发教程 - 开发者

    […] 这是解释http://tech.shantanugoel.com/2010/07/08/badtokenexception-android-dialog-getapplicationcontext.html。 […]

  42. pooja
    pooja at |

    Straight forward explaination.

  43. Hieu Pham
    Hieu Pham at |

    Thanks a lot, nice article, it saved my life 🙂

  44. Pedro Lacerda
    Pedro Lacerda at |

    Thanks! Worked for me too! 🙂

  45. Benson
    Benson at |

    just like that it worked, Thank you from South Africa

  46. Sakthi Priya
    Sakthi Priya at |

    Great ya! Thanks a lot!!! Its worked for me exacltly

Comments are closed.