Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AlertDialog The specified child already has a parent Exception

I have a little issue with AlertDialog on my application. I'm showing an AlertDialog so user can change the text of the button he just pressed. When I do that the first time there is no problem, but if I press the button again my application crashes with the Exception in the title. Here is the code which I'm using :

public void createDialog(){
     new AlertDialog.Builder(Settings.this)
    .setTitle("Stampii Server Name")
    .setView(input)
    .setPositiveButton("Set Name", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            String serverName = input.getText().toString();
            server.setText(serverName);
        }
    })
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    }).show();
}


server.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        createDialog();
    }
});

Any ideas how can I fix that problem? I've looked at the questions similar to this, but can't find a working solution.

Thanks in advance!

like image 997
Android-Droid Avatar asked Jan 18 '23 09:01

Android-Droid


1 Answers

.setView(input)

The variable "input" is not created in the method and is being added to a new dialog every time. This means, every time you call your create method, you are trying to add a new parent to the same object. You will need a new "input" every time you create the dialog, or you could use the same dialog over and over again.

like image 61
NotACleverMan Avatar answered Apr 05 '23 20:04

NotACleverMan