I ran into this issue recently. Googling around for the error led me to this thread<http://groups.google.com/group/android-developers/browse_thread/thread/7a648edddccf6f7d#> . Specifically, the response from Dianne: "One cause of this error may be trying to display an applicationwindow/dialog through a Context that is not an Activity."
As you already noted, getApplicationContext() and *this *(when called from an Activity) are NOT the same. I forget what it returns, but I make it a point to not use that function for anything anymore and pass around the Activity itself as the context where necessary to avoid stupid issues like this. Of course, this begs the question of what purpose getApplicationContext() serves and why the dialog builder doesn't take an Application instance instead of a Context if it requires one to function properly... * * On Tue, Nov 10, 2009 at 7:06 PM, Matt <[email protected]> wrote: > I tried > > b.create().show(); > > but that did not solve the problem. This is very strange. If I can use > 'getApplicationContext()' and 'this' interchangeably when dealing with > Toast messages, I assume that those two are in fact pretty much the > same. Now, on the other hand, when dealing with that > AlertDialog.Builder class, where I can use 'this', but using > 'getApplicationContext()' results in a runtime error when showing the > dialog, I must assume that 'this' and 'getApplicationContext()' are > NOT the same. How is that possible? > > Anyway, here is a error log. Maybe someone can think of a solution > while looking at the actual error message. > > ... > WARN/WindowManager(56): Attempted to add window with non-application > token WindowToken{43be4580 token=null}. Aborting. > DEBUG/AndroidRuntime(233): Shutting down VM > WARN/dalvikvm(233): threadid=3: thread exiting with uncaught exception > (group=0x4001b188) > ERROR/AndroidRuntime(233): Uncaught handler: thread main exiting due > to uncaught exception > ERROR/AndroidRuntime(233): android.view.WindowManager > $BadTokenException: Unable to add window -- token null is not for an > application > ... > > Oh, and thank you Justin for your effort! I really appreciate your > help! > > Best wishes, Matt > > > On 11 Nov., 00:51, Justin Anderson <[email protected]> wrote: > > This may seem weird, but try doing this: > > > > b.create().show(); > > > > I have never actually used the show method on the Builder class (I didn't > > realize it existed) and have never had any problems displaying a dialog. > > > > ---------------------------------------------------------------------- > > There are only 10 types of people in the world... > > Those who know binary and those who don't. > > ---------------------------------------------------------------------- > > > > On Tue, Nov 10, 2009 at 4:31 PM, Matt <[email protected]> wrote: > > > I should mention that the runtime error does not occur in this line > > > here > > > > > AlertDialog.Builder b = new AlertDialog.Builder(getApplicationContext > > > ()); > > > > > but when calling > > > > > b.show() > > > > > later on. According to DDMS, the WindowManager crashes because it is > > > trying to add a new Window (the dialog) with a token=null. How can > > > that be? Especially when the Toast message gets displayed properly and > > > that is created and shown even before creating that Builder instance. > > > I thought that maybe the application context wasn't fully initialized > > > yet, so I moved all the code to the onStart() method. Well, needless > > > to say that did not work either. Any clues? > > > > > Best wishes, Matt > > > > > On 10 Nov., 21:53, Justin Anderson <[email protected]> wrote: > > > > Shoot... I was kinda hoping they were in different parts of your > code. > > > I'm > > > > going to have to punt this one off to someone else... > > > > > > Anyone? > > > > > > > ---------------------------------------------------------------------- > > > > There are only 10 types of people in the world... > > > > Those who know binary and those who don't. > > > > > ---------------------------------------------------------------------- > > > > > > On Tue, Nov 10, 2009 at 1:27 PM, Matt <[email protected]> > wrote: > > > > > I have all the following code in my onCreate() method. > > > > > > > Toast.makeText(getApplicationContext(), "Hello World!", > > > > > Toast.LENGTH_SHORT).show(); > > > > > AlertDialog.Builder b = new > AlertDialog.Builder(getApplicationContext > > > > > ()); > > > > > > > [Dialog configuration] > > > > > > > b.show(); > > > > > > > While the Toast message gets displayed right away, the AlertDialog > > > > > won't. Instead there is a runtime error. Like I said, if you > replace > > > > > the "getApplicationContext()" with "this", it works flawlessly. > > > > > > > On 10 Nov., 20:44, Justin Anderson <[email protected]> > wrote: > > > > > > That's interesting. I've never run into that problem before. It > > > > > probably > > > > > > has something to do with what classes you are in or something > like > > > that. > > > > > > > > Here's a question... did you try the Toast in the same place > where > > > using > > > > > > AlertDialog.Builder gives an error or were they in different > parts of > > > > > your > > > > > > code? > > > > > > > > Thanks, > > > > > > Justin > > > > > ---------------------------------------------------------------------- > > > > > > There are only 10 types of people in the world... > > > > > > Those who know binary and those who don't. > > > > > ---------------------------------------------------------------------- > > > > > > > > On Tue, Nov 10, 2009 at 12:16 PM, Matt <[email protected]> > > > wrote: > > > > > > > Hi everybody, > > > > > > > > > can somebody please explain why > > > > > > > > > AlertDialog.Builder b = new > > > AlertDialog.Builder(getApplicationContext > > > > > > > ()); > > > > > > > > > will result in a runtime error, while > > > > > > > > > AlertDialog.Builder b = new AlertDialog.Builder(this); > > > > > > > > > will run just fine? I can use "this" and > "getApplicationContext()" > > > > > > > with Toast.makeText() interchangeably without any problems. > What is > > > > > > > different with that AlertDialog.Builder? Any ideas? > > > > > > > > > Cheers, Matt > > > > > > > > > -- > > > > > > > You received this message because you are subscribed to the > Google > > > > > > > Groups "Android Beginners" group. > > > > > > > To post to this group, send email to > > > > > [email protected] > > > > > > > To unsubscribe from this group, send email to > > > > > > > [email protected]<android-beginners%[email protected]> > <android-beginners%[email protected]<android-beginners%[email protected]> > > > > > <android-beginners%[email protected]<android-beginners%[email protected]> > <android-beginners%[email protected]<android-beginners%[email protected]> > > > > > > > > > <android-beginners%[email protected]<android-beginners%[email protected]> > <android-beginners%[email protected]<android-beginners%[email protected]> > > > > > <android-beginners%[email protected]<android-beginners%[email protected]> > <android-beginners%[email protected]<android-beginners%[email protected]> > > > > > > > > > > > For more options, visit this group at > > > > > > >http://groups.google.com/group/android-beginners?hl=en > > > > > > > -- > > > > > You received this message because you are subscribed to the Google > > > > > Groups "Android Beginners" group. > > > > > To post to this group, send email to > > > [email protected] > > > > > To unsubscribe from this group, send email to > > > > > [email protected]<android-beginners%[email protected]> > <android-beginners%[email protected]<android-beginners%[email protected]> > > > > > <android-beginners%[email protected]<android-beginners%[email protected]> > <android-beginners%[email protected]<android-beginners%[email protected]> > > > > > > > > > For more options, visit this group at > > > > >http://groups.google.com/group/android-beginners?hl=en > > > > > -- > > > You received this message because you are subscribed to the Google > > > Groups "Android Beginners" group. > > > To post to this group, send email to > [email protected] > > > To unsubscribe from this group, send email to > > > [email protected]<android-beginners%[email protected]> > <android-beginners%[email protected]<android-beginners%[email protected]> > > > > > For more options, visit this group at > > >http://groups.google.com/group/android-beginners?hl=en > > -- > You received this message because you are subscribed to the Google > Groups "Android Beginners" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected]<android-beginners%[email protected]> > For more options, visit this group at > http://groups.google.com/group/android-beginners?hl=en -- TreKing - Chiago transit tracking app for Android-powered devices http://sites.google.com/site/rezmobileapps/treking -- You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en

