The specified child already has a parent You must call removeView on the child s parent first Android

0 votes

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.

Feb 16, 2022 in Java by Soham
• 9,710 points
11,234 views

1 answer to this question.

0 votes

The error message says what You should do. Use the following lines of code and the ERROR won't occur:

// TEXTVIEW 
if(tv.getParent() != null) { 
  ((ViewGroup)tv.getParent()).removeView(tv); // <- fix 
} 
layout.addView(tv); // <========== ERROR IN THIS LINE DURING 2ND RUN 
// EDITTEXT

answered Feb 16, 2022 by Aditya
• 7,680 points

Related Questions In Java

0 votes
1 answer

How to call a method after a delay in Android using Java?

final Handler handler = new Handler(); handler.postDelayed(new Runnable() ...READ MORE

answered Jun 11, 2018 in Java by Akrati
• 3,190 points
5,337 views
0 votes
1 answer

No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK

In your Eclipse IDE, go into Window > Preferences > Java > Installed JREs > ...READ MORE

answered Jun 18, 2018 in Java by Parth
• 4,640 points
3,354 views
0 votes
1 answer

What is the maximum Java heap size of a 32-bit JVM on a 64-bit OS?

32-bit JVMs which expect to have a ...READ MORE

answered Nov 15, 2018 in Java by Frankie
• 9,830 points
2,883 views
0 votes
1 answer

How can I solve this: To prevent a memory leak, the JDBC Driver has been forcibly unregistered

What can you do? Ignore these warnings. Tomcat ...READ MORE

answered Jan 8, 2019 in Java by Daisy
• 8,140 points
31,562 views
0 votes
1 answer

Kotlin Convert Bitcoin value into Satoshi Value

java.lang.NumberFormatException: For input string: "45,586" This means ...READ MORE

answered Apr 12, 2022 in Blockchain by Soham
• 9,710 points
814 views
0 votes
1 answer

How can we get the current location in Android?

First you need to define a LocationListener to handle ...READ MORE

answered Sep 25, 2018 in Java by Parth
• 4,640 points
919 views
0 votes
1 answer

Can't find class CognitoUserPoolsSignInProvider: Issue with Sign In integration

CognitoUserPoolsSignInProvider is ditributed as part of aws-android-sdk-auth-userpools library. Please import ...READ MORE

answered Sep 28, 2018 in AWS by Priyaj
• 58,020 points
930 views
0 votes
1 answer

How do I resolve the "java.net.BindException: Address already in use: JVM_Bind" error?

If you are aware about what port ...READ MORE

answered Feb 22, 2022 in Java by Aditya
• 7,680 points
5,109 views
0 votes
1 answer

jQuery fix for "Uncaught TypeError: $ is not a function" error

 Use the following lines of code in ...READ MORE

answered Feb 16, 2022 in Java by Aditya
• 7,680 points
2,112 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP