Re: [android-beginners] Re: getApplicationContext() AlertDialog.Builder

2009-11-11 Thread Mark Murphy
Justin Anderson 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...

Since I'm being moderated (courtesy of a spammer forging my email
address, apparently), this message probably will show up a day late and
a euro short.

There is little reason anywhere to use getApplicationContext(). Activity
is a Context. Service is a Context. BroadcastReceivers get passed a
Context in onReceive(). And so on.

As to why the difference in behavior, my guess is that since Toasts live
outside any Activity, any sort of Context can be used to create them,
but Dialogs are more intrinsically tied to an Activity. Now, if that is
the case, it'd've been nice to actually make it be that way in the
parameters (take an Activity rather than a generic Context).

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

_Android Programming Tutorials_ Version 1.0 Available!

-- 
You received this message because you are subscribed to the Google
Groups Android Beginners group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en


[android-beginners] Re: getApplicationContext() AlertDialog.Builder

2009-11-11 Thread TreKing
Not sure why my last post did not go through ... let's try again...

I had this happen as well. Googling around I found this thread:

http://groups.google.com/group/android-developers/browse_thread/thread/7a648edddccf6f7d#

In particular, this note from Dianne:

One cause of this error may be trying to display an application
window/dialog through a Context that is not an Activity.

I'll bet if you saved a reference to whatever getApplicationContext()
returns and check the debugger you'll find that's it's not an
Activity. I've made it a point to not use getApplicationContext()
after this and pass the activity context around as necessary instead
to avoid stupid issues like this.

Why the dialog doesn't take an Activity if that's what it needs to
show properly is beyond me though ...

On Nov 10, 5:51 pm, Justin Anderson janderson@gmail.com 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 reisc...@googlemail.com 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 janderson@gmail.com 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 reisc...@googlemail.com 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 janderson@gmail.com 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 reisc...@googlemail.com
  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
android-beginners@googlegroups.com
  To unsubscribe from this group, send email to
  android-beginners+unsubscr...@googlegroups.comandroid-beginners%2bunsubscr...@googlegroups.com
  android-beginners%2bunsubscr...@googlegroups.comandroid-beginners%252bunsubscr...@googlegroups.com

android-beginners%2bunsubscr...@googlegroups.comandroid-beginners%252bunsubscr...@googlegroups.com
  

Re: [android-beginners] Re: getApplicationContext() AlertDialog.Builder

2009-11-11 Thread TreKing
I ran into this issue recently. Googling around for the error led me to this
threadhttp://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 reisc...@googlemail.com 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 janderson@gmail.com 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 reisc...@googlemail.com 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 janderson@gmail.com 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 reisc...@googlemail.com
 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 janderson@gmail.com
 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
 
   

[android-beginners] Re: getApplicationContext() AlertDialog.Builder

2009-11-11 Thread TreKing
I ran into this before.

Are you seeing token null is not for an application in the log cat?
Might be related to this thread:
http://groups.google.com/group/android-developers/browse_thread/thread/7a648edddccf6f7d
Specifically, this quote from Dianne:

One cause of this error may be trying to display an application
window/dialog through a Context that is not an Activity.

I'll bet if you check the type returned from getApplicationContext()
it's not the same as this.

On Nov 10, 2:53 pm, Justin Anderson janderson@gmail.com 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 reisc...@googlemail.com 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 janderson@gmail.com 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 reisc...@googlemail.com 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
  android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.comandroid-beginners%2bunsubscr...@googlegroups.com
  android-beginners%2bunsubscr...@googlegroups.comandroid-beginners%252bunsubscr...@googlegroups.com

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 android-beginners@googlegroups.com
  To unsubscribe from this group, send email to
  android-beginners+unsubscr...@googlegroups.comandroid-beginners%2bunsubscr...@googlegroups.com
  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 android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en


[android-beginners] Re: getApplicationContext() AlertDialog.Builder

2009-11-11 Thread Matt
@TreKing
I did get the exact same error (token null is not for an application).
Because of curiosity I put the following in my source code:

Log.v(TAG, this is  + this);
Log.v(TAG, getApplicationContext() is  + getApplicationContext());

I found that these two values are in fact NOT equal. While 'this'
points to the current Activity (e.g.
com.example.mypackage.MyActivityClass), 'getApplicationContext()'
returns a reference to an android.app.Application class. So you and
Dianne Hackborn are right. The problem is that the Dialog cannot
display properly because the provided Context is not an Activity.
Thank you so much! Finally some enlightenment.

@Mark Murphy
Your guess that the cause of the error could be the fact that Toast
lives outside any Activity, while Dialogs are tied to an Activity
proved absolutely right! I agree that it would be nice to reflect that
fact in the parameter list of those methods.

However, in the future I will also try to avoid using
'getApplicationContext()' if it can cause these annoying runtime
errors. In fact you two people helped me out tremendously. Thank you
very much for putting your time and thought into this.


Best wishes,
Matt



On 10 Nov., 22:49, TreKing treking...@gmail.com wrote:
 I ran into this before.

 Are you seeing token null is not for an application in the log cat?
 Might be related to this 
 thread:http://groups.google.com/group/android-developers/browse_thread/threa...
 Specifically, this quote from Dianne:

 One cause of this error may be trying to display an application
 window/dialog through a Context that is not an Activity.

 I'll bet if you check the type returned from getApplicationContext()
 it's not the same as this.

 On Nov 10, 2:53 pm, Justin Anderson janderson@gmail.com 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 reisc...@googlemail.com 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 janderson@gmail.com 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 reisc...@googlemail.com 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
   android-beginners@googlegroups.com
 To unsubscribe from this group, send email to
 android-beginners+unsubscr...@googlegroups.comandroid-beginners%2bunsubscr...@googlegroups.com
   android-beginners%2bunsubscr...@googlegroups.comandroid-beginners%252bunsubscr...@googlegroups.com

 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 android-beginners@googlegroups.com
   To unsubscribe from this group, send email to
   android-beginners+unsubscr...@googlegroups.comandroid-beginners%2bunsubscr...@googlegroups.com
   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 

[android-beginners] Re: getApplicationContext() AlertDialog.Builder

2009-11-10 Thread Matt
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 janderson@gmail.com 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 reisc...@googlemail.com 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 android-beginners@googlegroups.com
  To unsubscribe from this group, send email to
  android-beginners+unsubscr...@googlegroups.comandroid-beginners%2bunsubscr...@googlegroups.com
  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 android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en


[android-beginners] Re: getApplicationContext() AlertDialog.Builder

2009-11-10 Thread Matt
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 janderson@gmail.com 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 reisc...@googlemail.com 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 janderson@gmail.com 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 reisc...@googlemail.com 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
  android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.comandroid-beginners%2bunsubscr...@googlegroups.com
  android-beginners%2bunsubscr...@googlegroups.comandroid-beginners%252bunsubscr...@googlegroups.com

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 android-beginners@googlegroups.com
  To unsubscribe from this group, send email to
  android-beginners+unsubscr...@googlegroups.comandroid-beginners%2bunsubscr...@googlegroups.com
  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 android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en


Re: [android-beginners] Re: getApplicationContext() AlertDialog.Builder

2009-11-10 Thread Justin Anderson
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 reisc...@googlemail.com 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 janderson@gmail.com 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 reisc...@googlemail.com 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 janderson@gmail.com 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 reisc...@googlemail.com
 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
   android-beginners@googlegroups.com
 To unsubscribe from this group, send email to
 android-beginners+unsubscr...@googlegroups.comandroid-beginners%2bunsubscr...@googlegroups.com
 android-beginners%2bunsubscr...@googlegroups.comandroid-beginners%252bunsubscr...@googlegroups.com
 
   android-beginners%2bunsubscr...@googlegroups.comandroid-beginners%252bunsubscr...@googlegroups.com
 android-beginners%252bunsubscr...@googlegroups.comandroid-beginners%25252bunsubscr...@googlegroups.com
 
 
 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
 android-beginners@googlegroups.com
   To unsubscribe from this group, send email to
   android-beginners+unsubscr...@googlegroups.comandroid-beginners%2bunsubscr...@googlegroups.com
 android-beginners%2bunsubscr...@googlegroups.comandroid-beginners%252bunsubscr...@googlegroups.com
 
   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 android-beginners@googlegroups.com
 To unsubscribe from this group, send email to
 

[android-beginners] Re: getApplicationContext() AlertDialog.Builder

2009-11-10 Thread Matt
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 janderson@gmail.com 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 reisc...@googlemail.com 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 janderson@gmail.com 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 reisc...@googlemail.com 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 janderson@gmail.com 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 reisc...@googlemail.com
  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
android-beginners@googlegroups.com
  To unsubscribe from