Skip to content Skip to sidebar Skip to footer

Which Context Do I Need?

I'm creating a dialog box and using the (this) isnt working. Up until now its just been a button calling a dialogbox but now the button within the called dialogbox needs to call an

Solution 1:

getApplicationContext() or use YourActictyName.this Because this refers the button click listner ,not your class Object

Solution 2:

If this code is in the onCreate() method, or similiar, add getApplicationContext() instead of this and you should be fine. That's because this in a Button-context will refer to the button environment.

Solution 3:

To improve the isolation between the two dialogs, it would be best to call showDialog(R.id.delapptcon) from the onClick handler. Then load the new dialog in the onCreateDialog of your activity. In this way, you can create more reusable dialogs and avoid the scoping issue you have now.

Post a Comment for "Which Context Do I Need?"