简体   繁体   中英

AlertDialog to pop up on button click

I have a TableLayout for which I want the user to, on runtime, be able to add items (and naming these items with a single EditText). Thus I need an "Add"-button which I thought an AlertDialog would be suited for. Have the standard code from the android developers tutorial:

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater inflater = this.getLayoutInflater();
    builder.setView(inflater.inflate(R.layout.add_layout,null));
    builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // User clicked OK button
               }
           });
    builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // User cancelled the dialog
               }
           });
    AlertDialog dialog = builder.create();
    dialog.show();

which works perfectly when putting it in the Activity´s onCreate method but not when inserting it in the onClick-method for this "Add"-button. Why is that and how can I resolve this? A little more information: the Add-buttons onClick method is called through the activity´s XML file, android:onClick="Add", which then is a function in the activity´s java/class file. What happens when clicking on the Add button is just that the Activity crashes, not the whole app, and I get tossed back one step to the parent activity with the message: "Application has unfortunately been stopped".

I think you may not have had much response to this question yet because you've not provided enough information. You say the AlertDialog doesn't work when you put it in the onClick method for your Add button. What happens? Does the app crash? If so what error messages do you get? Or does it not crash, but no AlertDialog gets shown? How are you putting the AlertDialog in the onClick method? Are you using the android:onClick="someMethod" attribute in your layout xml, or are you assigning an onClickListener in your code?

I'll take a guess and assume you are assigning an onClickListener in your code in a form something like the following:

myAddButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = this.getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.add_layout,null));
        ...
    }
});

If you are taking the above approach you need to realise you are using an anonymous inner class, and that the this keyword refers to the object it is in. In the anonymous inner class, the this keyword no longer refers to the parent activity that the anonymous inner class has been put in.

The AlertDialog.Builder() constructor expects you to pass it a context. When you write AlertDialog.Builder(this) in the onCreate() method of your Activity, this refers to the Activity, and an Activity is a subclass of Context, so the AlertDialog.Builder() constructor is happy.
When you write AlertDialog.Builder(this) in your View.OnClickListener() however, this now refers to the onClickListener, which is not a subclass of Context, so the AlertDialog.Builder() constructor is not happy.

If this is not the approach you are taking, then please provide more information so it is possible to help you.

If this IS the approach you are taking, then you can overcome the problem by putting the AlertDialog creation back in the Activity.

...
myAddButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        showMyAlertDialog(); // Call a method in your Activity
    }
});
...

// Create a method in your activity
private void showMyAlertDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);  // <-- 'this' now refers to the Activity
    LayoutInflater inflater = this.getLayoutInflater();
    builder.setView(inflater.inflate(R.layout.add_layout,null));
    ...
}

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