Android - The specified child already has a parent. You must call removeView() on the child's parent first
<p>I know there are dozens of questions that ask about this error, but none of the proposed solutions seem to apply to my problem, at least that I see.</p>

<p>Here's my log:</p>

<pre><code>java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    at android.view.ViewGroup.addViewInner(ViewGroup.java:1976)
    at android.view.ViewGroup.addView(ViewGroup.java:1871)
    at android.view.ViewGroup.addView(ViewGroup.java:1851)
    at com.android.internal.app.AlertController.setupView(AlertController.java:365)
    at com.android.internal.app.AlertController.installContent(AlertController.java:206)
    at android.app.AlertDialog.onCreate(AlertDialog.java:251)
    at android.app.Dialog.dispatchOnCreate(Dialog.java:307)
    at android.app.Dialog.show(Dialog.java:225)
    at com.company.MyApp.MyActivity$7.onItemClick(MyActivity.java:240)
    at android.widget.AdapterView.performItemClick(AdapterView.java:284)
    at android.widget.ListView.performItemClick(ListView.java:3513)
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3683)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    at dalvik.system.NativeStart.main(Native Method)
</code></pre>

<p>Here is MyActivity.java onCreate(). You can see that a Dialog Builder is shown and given some values. Both <code>myDialogLayout</code> and <code>myDialogBuilder</code> are private class members</p>

<pre><code>@Override
public void onCreate( Bundle savedInstanceState )
{
    // ... a bunch of init code...

    // Create a dialog builder
    myDialogLayout = getLayoutInflater().inflate(R.layout.leaving, null);
    myDialogBuilder = new AlertDialog.Builder(this)
        .setTitle("My Title")
        .setView(myDialogLayout)
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener()
        {
            public void onClick( DialogInterface dialog, int which ) {}
        });

    // ... more code ...
}
</code></pre>

<p>Later, when a certain button is clicked in my Activity, the <code>myDialogBuilder</code> is customized a bit and then a dialog is created and shown. <code>myDialog</code> is a private class variable/member:</p>

<pre><code>// Builder customized a bit
myDialogBuilder 
    .setMessage("custom message here");

// Dialog created from Builder
myDialog = myDialogBuilder.create();

// Dialog shown
myDialog.show();  // &lt;---- MyActivity.java Line: 240
</code></pre>

<p>So onto my problem, when I click my button, the dialog is created successfully. But after I press Cancel on the dialog and then press the button again, I get the error seen in the log. For some reason, the activity isn't wanting me to reuse my <code>myDialog</code> dialog. It's a class member, so it is accessible from the <code>onClick</code> handler. And the <code>myDialog</code> is a new dialog every time the button is clicked, because it's created from scratch by the <code>myDialogBuilder.create()</code> every time.</p>

<p>Anyone know what the problem is? I also tried adding in <code>myDialog.dismiss()</code> when the Cancel button is pressed but that didn't make a difference.</p>

<p>Also, you can see in my <code>myDialogBuilder</code> that a custom XML layout is used for the Dialog's view. According to the error message, it sounded like it wants me to use <code>removeView()</code> in order to remove the view from being used in the Dialog. But <code>myDialogBuilder.removeView()</code> isn't a valid method.</p>
