0

When I create a dialog upon a Button click, I get an error. How can I show a dialog upon a Button click?

My main class extends Activity.

            deleteentry.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    new deleteOptionsDialog(getApplicationContext()).show();
                }
            });
    public class deleteOptionsDialog extends Dialog {

            public deleteOptionsDialog(final Context context) {
                super(context, android.R.style.Theme_Translucent);

                requestWindowFeature(Window.FEATURE_NO_TITLE);
                setContentView(R.layout.delete_options_dialog);

                RelativeLayout cameraLayout = (RelativeLayout) findViewById(R.id.rldelete);
                cameraLayout.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                    }
                });

            }
Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
user3114723
  • 401
  • 1
  • 6
  • 16
  • what error you are getting ? – amit singh Feb 10 '14 at 12:43
  • Your question **how can i call dialog method in button clicking** but in your code u can implement click in `RelativeLayout`. Where is u r button? – M D Feb 10 '14 at 12:45
  • see here http://stackoverflow.com/questions/2634991/android-1-6-android-view-windowmanagerbadtokenexception-unable-to-add-window and http://stackoverflow.com/questions/4487491/android-unable-to-add-window-token-null-is-not-for-an-application and http://stackoverflow.com/questions/5796611/dialog-throwing-unable-to-add-window-token-null-is-not-for-an-application-wi and here http://stackoverflow.com/questions/18662239/android-view-windowmanagerbadtokenexception-unable-to-add-window-on-buider-s... – Gopal Gopi Feb 10 '14 at 12:50

3 Answers3

0

replace

new deleteOptionsDialog(getApplicationContext()).show();

with

new deleteOptionsDialog(YourActivity.this).show();

and try to move all your code inside Constructor to onCreate() method of Dialog by overriding it.

Gopal Gopi
  • 11,317
  • 1
  • 27
  • 43
0

try use new deleteOptionsDialog(YourCurrentActivity.this).show(); Problem in use getApplicationContext() in creating dialog

M D
  • 46,860
  • 8
  • 87
  • 108
a.black13
  • 2,107
  • 4
  • 17
  • 20
0

For Dialog instance you should use Activity context, not getApplicationContext() and not getApplication(), but YourActivity.this

And it's a better idea to instanciate dialog somewhere in onCreate() an later check if it is showing when trying to show once more

Sam
  • 1,594
  • 16
  • 25