Question: So I have a button that displays an alert dialog when its clicked. 
I create the view for the alert dialog in the onCreate method of my activity.
The code for that is right here:

```
    LayoutInflater factory = LayoutInflater.from(this);
    view = factory.inflate(R.layout.grade_result, null);

```

When I push the button for the first time, the dialog displays the way I want it, but when I push it a second time it throws this exception.

11-28 00:35:58.066: E/AndroidRuntime(30348): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

My code for the method that displays the AlertDialog when the button is pushed is right here:

```
public void details(View v){
    final AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setView(view);
    alert.setMessage("Details About Your Grades")
    .setCancelable(false)
    .setPositiveButton("Continue", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id){
            dialog.cancel();

        }
    });
    alert.show();

```

Any help would be appreciated!
Thank you!

Answer: 
It says that your "view" was connected to parent. It's happend when you called .setView(view). If i'm right - alertDialog class gives parent for your view. It is basics for android. ViewGroup may have a lot of child View's. I had similar problem.
So, you can try this insinde onClick:

```
ViewGroup vGroup = view.getParent();
vGroup.removeView(view);
// And right after that - close dialog
dialog.dismiss();

```

It helped me, I hope it'll help you!
Use this :

```
final AlertDialog.Builder alert = new AlertDialog.Builder(ProductsListAfterFilter.this);
alert.setTitle("Добавить продукт в корзину?");
alert.setMessage("Введите количество: ");

// Set an EditText view to get user input
final EditText input = new EditText(ProductsListAfterFilter.this);
alert.setView(input);

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
        int value = 0 ;
    try {
            value = Integer.valueOf(input.getText().toString());
    }
    catch (Exception err){
            Toast.makeText(getApplicationContext(), "Введите число", 100).show();
    }
    //insertValue(value,st1,st2,st3,st4);
    Toast.makeText(getApplicationContext(), "value = "+value, 100).show();
    dialog.dismiss();
    dialog.cancel();
    }
});

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
    // Canceled.
        dialog.cancel();
        dialog.dismiss();

    }
});

final AlertDialog alertd = alert.create();
alertd.show();

```