android – open a dialog when i click a button-ThrowExceptions

Exception or error:

I have a button and I would like to open a dialog when pressed. This is my code:

Button more = (Button) findViewById(R.id.more);
more.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        //Intent myIntent = new Intent(view.getContext(), agones.class);
        //startActivityForResult(myIntent, 0);

        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("hi");
        alertDialog.setMessage("this is my app");

        alertDialog.setButton("Continue..", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // here you can add functions
        }
        });
    }
});
How to solve:

As @Roflcoptr has said, you haven’t called alertDialog.show() method. thus your dialog doesn’t appear.

Here’s your edited code:

Button more = (Button) findViewById(R.id.more);
more.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        //Intent myIntent = new Intent(view.getContext(), agones.class);
        //startActivityForResult(myIntent, 0);


        AlertDialog alertDialog = new AlertDialog.Builder(<YourActivityName>this).create(); //Read Update
        alertDialog.setTitle("hi");
        alertDialog.setMessage("this is my app");

        alertDialog.setButton("Continue..", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int which) {
              // here you can add functions
           }
        });

        alertDialog.show();  //<-- See This!
    }

});

if you write this instead of <ActivityName>.this, then it is going to take the reference of View.OnClickListener since this is currently being accessed inside it. You need to give your Activity’s name there.

###

Your dialog isn’t displayed, because you don’t call AlertDialog#show.

###

            final AlertDialog.Builder builder = new AlertDialog.Builder(this);

            builder.setMessage("this is message");
            builder.setTitle("this is title");

            //Setting message manually and performing action on button click
            builder.setMessage("Do you want to close this application ?");T
            //This will not allow to close dialogbox until user selects an option
            builder.setCancelable(false);
            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            Toast.makeText(this, "positive button", Toast.LENGTH_SHORT).show();
                            //builder.finish();
                        }
                    });
             builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            //  Action for 'NO' Button
                            Toast.makeText(this, "negative button", Toast.LENGTH_SHORT).show();
                            dialog.cancel();
                        }
                    });

            //Creating dialog box
            AlertDialog alert = builder.create();
            //Setting the title manually
            //alert.setTitle("AlertDialogExample");
            alert.show();

Leave a Reply

Your email address will not be published. Required fields are marked *