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. Helo with onActivityResult()

I am starting a sub activity that browses for a file, then in the
onActivityResult() method I parse the file that is returned.  The trouble is
that the parsing takes a little while and I am trying to pop up a progress
dialog, but the file browser is visible until the onActivityResult() method
finishes.  Is there some way that I can force the file browser to go away
and show the calling activity so the progress dialog will show?

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

2. Open GL Tutorial problem

Hello Group my name is Claudio Veas and Im having a problem while
trying to execute the tutorial about Android with OPEN GL. The
application consist of two activities one is for selecting the
tutorials and then this activity calls another that according to the
selection of the tutorial, it creates the view that contains the
selected tutorial. The problem is that I have never got to see any of
the tutorials running. I tried debuging and for sure the second
activity is being executed and the right tutorial is selected but when
the application tries to run the constructor of the view this sentence
causes a problem so big that the activity is closed and the
application goes back to the previous one (initial activity) .

package edu.union;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.OpenGLContext;
import android.view.View;

public class GLTutorialOne extends View {
        private OpenGLContext glContext;

        public GLTutorialOne(Context c) {
                super(c);
                glContext = new OpenGLContext(0);
                GL10 gl = (GL10)glContext.getGL();

                gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //<- THIS ONE IS THE 
ONE
THAT SEEMS TO BE  //CAUSEING PROBLEMS
        }

        protected void onDraw(Canvas canvas) {
                GL10 gl = (GL10)glContext.getGL();

                glContext.waitNative();

                gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        }
}

This is where you can download the tutorial and aparently for what I
have read in some places these tutorials worked fine in M3 version of
the sdk but had some problems in m5  version (I had to make some
changes in my code to make it work). Any Help will be welcome and if
you require more information do not hesitate to ask.

source are in http://code.google.com/p/android-gl/downloads/list

Thanks alot
Claudio Veas

PS: Please excuse my english is not  very good.
--~--~---------~--~----~------------~-------~--~----~

3. TextView Error (ERROR/TextView: Need text Layout but do not have a width)

4. AndNav - Update

5. Android Tutorial - Option and Context menu

6. Android Tutorial - XML View inflation

7. gallery question