title: The specified child already has a parent. You must call removeView() on the child's parent first (Android)
body: <p>I have to switch between two layouts frequently. The error is happening in the layout posted below.</p>
  
  <p>When my layout is called the first time, there doesn't occur any error and everything's fine. When I then call a different layout (a blank one) and afterwards call my layout a second time, it throws the following error:</p>
  
  <pre><code>&gt; FATAL EXCEPTION: main
  &gt;     java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
  </code></pre>
  
  <p>My layout-code looks like this:</p>
  
  <pre><code>    tv = new TextView(getApplicationContext()); // are initialized somewhere else
      et = new EditText(getApplicationContext()); // in the code
  
  
  private void ConsoleWindow(){
          runOnUiThread(new Runnable(){
  
       @Override
       public void run(){
  
          // MY LAYOUT:
          setContentView(R.layout.activity_console);
          // LINEAR LAYOUT
          LinearLayout layout=new LinearLayout(getApplicationContext());
          layout.setOrientation(LinearLayout.VERTICAL);
          setContentView(layout);
  
          // TEXTVIEW
          layout.addView(tv); //  &lt;==========  ERROR IN THIS LINE DURING 2ND RUN
          // EDITTEXT
          et.setHint("Enter Command");
          layout.addView(et);
          }
      }
  }
  </code></pre>
  
  <p>I know this question has been asked before, but it didn't help in my case.</p>