I am being required to constantly switch between two layouts frequently and despite that, the error is occurring in the layout mentioned below and also when my layout is called the ‘First Time’, then no errors occur and everything works fine but however, when I call a blank layout and later name it my ‘Second Time’, I receive the ERROR mentioned below:-
> FATAL EXCEPTION: main
> java.lang.IllegalStateException:
The specified child already has a parent. You must call removeView() on the child's parent first.
My layout-code looks like this:
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); // <==========
ERROR IN THIS LINE DURING 2ND RUN
// EDITTEXT et.setHint("Enter Command");
layout.addView(et);
}
}
}
I know this question has been asked before, but it didn't help me in this case.