In my application, I often have to switch between two layouts. The error occurs in the layout below.
When my layout is called for the first time, no error occurs, and everything is in order. When I then call another layout (empty) and then call my layout a second time, it gives me the following error:
> FATAL EXCEPTION: main > java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child 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 already been asked, but in my case it did not help.
java android android-edittext textview parent-child
blackst0ne Jan 21 '15 at 15:50 2015-01-21 15:50
source share