Calling an AlertDialog from inside onOptionsItemSelected

by Bret Foreman » Wed, 21 Jul 2010 10:00:25 GMT


Sponsored Links
 I'm getting an exception when I try to pop up an alert dialog from
within the onOptionsItemSelected method of an Activity. The exception
looks like this:

W/WindowManager(   57): Attempted to add window with non-application
token WindowToken{43c4d3b0 token=null}.  Aborting.

I pasted the same code into a button's onClick method and it worked
fine. Any ideas?

Here's the code:

                        AlertDialog alertDialog = new
AlertDialog.Builder(getApplicationContext()).create();
                        alertDialog.setTitle(R.string.confirmClear);
                        alertDialog.setMessage(getString(R.string.areYouSure));
                        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE,
getString(R.string.yes), new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int 
which) {
                                /*
                                * This is where we clear all the events from 
the database.
                                */
                                // do something
                        } });
                        alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE,
getString(R.string.no), new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int 
which) {
                               // don't do anything
                            return;
                        } });
                        alertDialog.show();

--



Calling an AlertDialog from inside onOptionsItemSelected

by Mark Murphy » Wed, 21 Jul 2010 10:05:08 GMT


 Get rid of all occurrences of getApplicationContext() from your code,
including the one shown in the snippet below. Use "this" instead
(minus the quotes), as that will be a perfectly valid Context, one
much better suited for your purposes.



>


Sponsored Links


Calling an AlertDialog from inside onOptionsItemSelected

by Bret Foreman » Wed, 21 Jul 2010 10:14:31 GMT


 That worked. Mark, you rock...and after 7pm too!

--



Calling an AlertDialog from inside onOptionsItemSelected

by Mark Murphy » Wed, 21 Jul 2010 10:19:36 GMT


 


What? People are tracking my sleeping habits now?

:-)

I was probably a bit over the top by saying to delete all
getApplicationContext() calls, though there's so few cases where that
is the right method to use, I can't think of any of them off the top
of my head. Certainly if you're trying to do anything with the GUI,
getApplicationContext() is lobotomized -- your Activity is a far
better Context for methods that need one.

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

_The Busy Coder's Guide to Android Development_ Version 3.1 Available!

--



Other Threads

1. help! adding an activity of my application to another application

Hi Guys,

After a picture is taken in Android, you get a menu to save it, etc.
Is it possible to add a menu item in it saying "Upload pic to xyz.com"
and then have it launch an activity of my app ?

Thanks,

Moazzam
--~--~---------~--~----~------------~-------~--~----~

2. Cursor position at the end of text

Hi,
      When i try to edit and already stored notepad , i see the cursor
at the end of first line. I want to make it appear to the end of the
text.

Please help me in this.

Thanks for your support !!


--~--~---------~--~----~------------~-------~--~----~

3. Time to first screen, boot-up time and app-start time?

4. LocationManager always returns null ... ?

5. what is the fast way to save/load string from/in an array?

6. java question

7. Accessing logs on Android