简体   繁体   中英

Why does my alert dialog not appear?

Can sombody please help me? When I click on the button nothing happens. I'm very new to android-programming so please answer as i can understand.

(Don't wonder about my variables)

Thank you

@Override
public void onClick(View v) {

    Button preis = (Button) findViewById(R.id.essenpreis);
    preis.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            // Creating alert Dialog with one Button

            AlertDialog.Builder alertDialog = new AlertDialog.Builder(options.this);

            // Setting Dialog Title
            alertDialog.setTitle("Essenspreis");

            // Setting Dialog Message
            alertDialog.setMessage("Neuen Preis eintragen:");

            // Setting Icon to Dialog
            // alertDialog.setIcon(R.drawable.tick);

            // Setting OK Button
            alertDialog
                .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int which) {

                            // Write your code here to execute after dialog closed
                        Toast.makeText(getApplicationContext(),"Preis geändert!", Toast.LENGTH_SHORT).show();
                        }
                    });

            // Showing Alert Message
            alertDialog.show();

        }
    });
  }                 
}

Your clicklistener for button is defined inside the method body of onClick interface thats why your dialog is not showing,

This is how to show an alertDialog

           Button preis = (Button) findViewById(R.id.essenpreis);

        // add button listener
        preis.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);

            // set title
            alertDialogBuilder.setTitle("Your Title");

            // set dialog message
            alertDialogBuilder
                .setMessage("Click yes to exit!")
                .setCancelable(false)
                .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, close
                        // current activity
                        MainActivity.this.finish();
                    }
                  })
                .setNegativeButton("No",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, just close
                        // the dialog box and do nothing
                        dialog.cancel();
                    }
                });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();
            }
        });
    }

Note that you are setting listener to button with id R.id.essenpreis after first onClick method is performed, check if you are assigning that click listener to anyone, your code work if you extract the button setup from the first onClick

        Button preis = (Button) findViewById( android.R.id.button1 );
        preis.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                // Creating alert Dialog with one Button

                AlertDialog.Builder alertDialog = new AlertDialog.Builder( MainActivity.this );

                // Setting Dialog Title
                alertDialog.setTitle("Essenspreis");

                // Setting Dialog Message
                alertDialog.setMessage("Neuen Preis eintragen:");

                // Setting Icon to Dialog
                // alertDialog.setIcon(R.drawable.tick);

                // Setting OK Button
                alertDialog
                    .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int which) {

                                // Write your code here to execute after dialog closed
                            Toast.makeText(getApplicationContext(),"Preis geändert!", Toast.LENGTH_SHORT).show();
                            }
                        });

                // Showing Alert Message
                alertDialog.show();

            }
        }); 

I am assuming that the options.java is your main source file.

If you have created a layout in XML, then please give id to the button as:

android:id = "@+id/button1"

If you haven't created any layout then create a file main.xml in res folder as follows

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/buttonAlert"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Show Alert Box" />

</LinearLayout>

Now in the activity, I suppose here it is Options.java from the code.

in the onCreate method write the following code :

Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            options.this);

        // set title
        alertDialogBuilder.setTitle("Essenspreis");

        // set dialog message
        alertDialogBuilder
            .setMessage("Neuen Preis eintragen:")
            .setCancelable(false)
            .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, close

                }
              })
            .setNegativeButton("No",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, just close

                }
            });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM