title: I keep getting a "The specified child already has a parent" error for my Kotlin code
body: <p>I'm trying to make a quiz app where you click the plus button to add a quiz. A button pops up on the screen and you can click that to either play or edit the quiz. When I click the  newly created button once its fine but the second time I click it, the app crashes. Does anyone have any idea how I can fix this error specifically?</p>
  <p>Main Activity page:</p>
  <pre><code>    package com.example.k_ari
  
  import android.content.Intent
  import android.graphics.Color
  import androidx.appcompat.app.AppCompatActivity
  import android.os.Bundle
  import android.util.Log
  import android.view.Gravity
  import android.view.ViewGroup
  import android.widget.Button
  import android.widget.EditText
  import android.widget.LinearLayout
  import androidx.appcompat.app.AlertDialog
  import com.google.android.material.floatingactionbutton.FloatingActionButton
  
  class MainActivity : AppCompatActivity() {
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
  
          showEditTextDialogue()
      }
  
      private fun showEditTextDialogue() {
          val btn = findViewById&lt;FloatingActionButton&gt;(R.id.btn)
          btn.setOnClickListener{
  
              val inflator = layoutInflater
              val layout = findViewById&lt;LinearLayout&gt;(R.id.mainlayout)
              val dialogLayout = inflator.inflate(R.layout.edit_text_layout, null)
              val editText = dialogLayout.findViewById&lt;EditText&gt;(R.id.et_editText)
              val builder = AlertDialog.Builder(this)
              val newbutton = Button(this@MainActivity)
              val btnintent = Intent(this, Newbtn::class.java)
              val dialogLayout2 = inflator.inflate(R.layout.new_button_option, null)
  
              newbutton.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
              newbutton.width=1010
              newbutton.height=300
              newbutton.gravity = Gravity.CENTER
              newbutton.translationX= 65F
              newbutton.setTextColor(Color.parseColor(&quot;#FFFFFFFF&quot;))
              newbutton.setBackgroundColor(Color.parseColor(&quot;#250A43&quot;))
  
              with(builder) {
                  setTitle(&quot;Title:&quot;)
                  setPositiveButton(&quot;Ok&quot;){dialog, which -&gt;
                      layout.addView(newbutton)
                      newbutton.text = editText.text.toString()
                  }
                  setNegativeButton(&quot;Cancel&quot;){dialog, which -&gt;
                      Log.d(&quot;Main&quot;, &quot;Negative button clicked&quot;)
                  }
                  setView(dialogLayout)
                  show()
              }
              newbutton.setOnClickListener{
                  with(builder){
                      setPositiveButton(&quot;Play&quot;){dialog, which -&gt;
                          startActivity(btnintent)
                      }
                      setPositiveButton(&quot;Edit&quot;){dialog, which -&gt;
                          startActivity(btnintent)
                      }
                      setView(dialogLayout2)
                      show()
              }
          }
      }
  }}
  </code></pre>
  <p>Xml file:</p>
  <pre><code> &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:orientation=&quot;vertical&quot;
      tools:context=&quot;.MainActivity&quot;&gt;
  
      &lt;com.google.android.material.floatingactionbutton.FloatingActionButton
          android:id=&quot;@+id/btn&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;56dp&quot;
          android:backgroundTint=&quot;#250A43&quot;
          android:backgroundTintMode=&quot;add&quot;
          android:clickable=&quot;true&quot;
          android:contentDescription=&quot;@string/todo&quot;
          android:tint=&quot;#250A43&quot;
          android:translationX=&quot;280dp&quot;
          android:translationY=&quot;580dp&quot;
          app:backgroundTint=&quot;#250A43&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintHorizontal_bias=&quot;0.56&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toTopOf=&quot;parent&quot;
          app:rippleColor=&quot;#FFFFFF&quot;
          app:srcCompat=&quot;@android:drawable/ic_input_add&quot;
          android:focusable=&quot;true&quot; /&gt;
      &lt;ScrollView
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;&gt;
  
          &lt;LinearLayout
              android:layout_width=&quot;match_parent&quot;
              android:id=&quot;@+id/mainlayout&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:orientation=&quot;vertical&quot;&gt;
          &lt;/LinearLayout&gt;
      &lt;/ScrollView&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  <p>Logcat:</p>
  <blockquote>
  <p>2020-12-28 13:48:47.521 26648-26648/com.example.k_ari E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.example.k_ari, PID: 26648
  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:5458)
  at android.view.ViewGroup.addView(ViewGroup.java:5277)
  at android.view.ViewGroup.addView(ViewGroup.java:5249)
  at androidx.appcompat.app.AlertController.setupCustomContent(AlertController.java:657)
  at androidx.appcompat.app.AlertController.setupView(AlertController.java:475)
  at androidx.appcompat.app.AlertController.installContent(AlertController.java:233)
  at androidx.appcompat.app.AlertDialog.onCreate(AlertDialog.java:279)
  at android.app.Dialog.dispatchOnCreate(Dialog.java:708)
  at android.app.Dialog.show(Dialog.java:422)
  at androidx.appcompat.app.AlertDialog$Builder.show(AlertDialog.java:1009)
  at com.example.k_ari.MainActivity$showEditTextDialogue$1$2.onClick(MainActivity.kt:66)
  at android.view.View.performClick(View.java:8178)
  at android.widget.TextView.performClick(TextView.java:16104)
  at android.view.View.performClickInternal(View.java:8147)
  at android.view.View.access$3700(View.java:888)
  at android.view.View$PerformClick.run(View.java:30233)
  at android.os.Handler.handleCallback(Handler.java:938)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:246)
  at android.app.ActivityThread.main(ActivityThread.java:8414)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:596)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)</p>
  </blockquote>