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. create images from jpg files

Hello,
i currently have a List<Files> which are all jpg format.  I am trying
to traverse through this list displaying each image using the
ImageView class.  I'm having a problem finding a way to create an
image to display out of the contents of the list.  I am new to android
and the java language :-/   I have tried using createBitmap but it
seems Bitmap doesn't really work with Files either.  Any ideas?

here is the function..

public View getView(int position, View convertView, ViewGroup parent)
{
            ImageView i = new ImageView(mContext);

            i.setImageBitmap(thumbs.get(position));
            i.setAdjustViewBounds(true);
            i.setLayoutParams(new Gallery.LayoutParams(
                    LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
            return i;
        }

-- 

2. Uploading Files in Nexus One

I was trying to upload files (images) to my wordpress blog through my
Nexus One mobile phone, but it shows 'Disabled' all the time, whether
I use the flash upload or the regular HTML upload contorl. I even
tried the Mobile admin Wordpress plugin with the same results.

Is there any option to enable upload files to wordpress using my Nexus
One phone, some settings that I need to change maybe ?


Idan

-- 

3. problems using the Android Manifest Editor

4. Gallery crashing when scrolling using trackball

5. Empty contact list after adding contacts via insert query

6. icons bigger than 48x48 pixels

7. Notepad tutorial revised with hosted database