@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.com<android-beginners%2bunsubscr...@googlegroups.com>
> > > <android-beginners%2bunsubscr...@googlegroups.com<android-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.com<android-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

Reply via email to