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. android.telephony.SmsManager.sendTextMessage throw exception when destination addr is email addr

Check the length of sMessage, if the message body is too long it may course
this exception. Try to split your message body into shot pieces. Good luck!

2010/2/4 sherry <syang...@gmail.com>





> 

2. Online survey (telcosurvey) -- berhadiah Android cuy... :D

Rekans,

Saya ada titipan dari teman nih untuk membantu mengisi online survey dari 
TelcoSurvey.

Lumayan ada hadiah nya :D , yang jelas lembaga survey nya kredibel kok.. :D

thx,

+++++++++++++++++++++


IKUTI SURVEY ONLINE TELCO 2010 YANG DI ADAKAN OLEH TELCOSURVEY.

Survey online ini bertujuan untuk mengetahui kualitas dan performa layanan yang 
diberikan provider telekomunikasi di Indonesia.

Dapatkan hadiah utama Android SE X8 dan hadiah hiburan berupa pulsa yang akan 
ditransfer langsung ke handphone anda.

Kami mengharapkan anda mengisi data dengan jujur dan benar agar survey ini 
dapat berguna untuk meningkatkan kualitas layanan GSM. Silakan 
mem-forward/meneruskan link survey online ini ke seluruh jaringan anda di 
Indonesia.

Silahkan buka www.telcosurvey.com 

Thx, --------------------------------
email: I have a lot
BB PIN: Always change
Just twit me @dollysw
Sent using trusted network, @XL123

-- 
===============
"Indonesian Android Community [id-android]"

PING'S mobile
Email: sa...@pings-mobile.com  Ph. (021) 96087100
---------------------
Yopie Ratjoen
Email: yo...@ratjoen.com  Ph. 08977726681
--------------------
Gila original
Email: i...@gilaoriginal.com  Ph. (031) 91555898
--------------------

Aturan Jual/Kloteran ID-Android  http://goo.gl/azW7

3. Error after booting Froyo

4. glTexImage2D is too slow,

5. problem in transfering files to sd card

6. Android build issue

7. OOT: Let's Tango!