The specified child already has a parent. You must call removeView() on the child's parent first (Android)
The error message says what You should do.
// TEXTVIEW
if(tv.getParent() != null) {
((ViewGroup)tv.getParent()).removeView(tv); // <- fix
}
layout.addView(tv); // <========== ERROR IN THIS LINE DURING 2ND RUN
// EDITTEXT
Kotlin : The specified child already has a parent. You must call removeView() on the child's parent first
You're trying to add the LinearLayout
with the ID root_layout
as its own child here:
layout.addView(layout)
Perhaps you meant to add your newly inflated View
as its child?
layout.addView(view)
Activity: You must call removeView() on the child's parent first
You're adding the same view multiple times...
for (...) {
// can't add _same_ view multiple times!
gridLayout.addView(profileCategoryActive);
}
A view can only have one parent / be added to one other view, hence the exception.
If you want to add multiple views to a layout, then you need to inflate multiple views, and add them each separately.
for (...) {
View view = inflate(..) // inflate new view
layout.addView(view); // add new view
}
Call removeView() on the child's parent first
Solution:
((ViewGroup)scrollChildLayout.getParent()).removeView(scrollChildLayout);
//scrollView.removeView(scrollChildLayout);
Use the child element to get a reference to the parent. Cast the parent to a ViewGroup so that you get access to the removeView method and use that.
Thanks to @Dongshengcn for the solution
The specified child already has a parent. You must call removeView() on the child's parent first. Facing this issue
Seems like this was reported and according to the comment in there, this should be fixed for preview 11+: github.com/dotnet/maui/issues/3511 Which VS2022 have you installed?
Installing Visual Studio 2022 v17.1 Preview 2 should give you .NET MAUI Preview 11 which should resolve the issue.
Related Topics
Scroll Recyclerview to Show Selected Item on Top
Asynctask Won't Stop Even When the Activity Has Destroyed
Slowing Speed of Viewpager Controller in Android
How to Get the Height and Width of the Android Navigation Bar Programmatically
Find Distance Between Two Points on Map Using Google Map API V2
Which Is the Best Way to Add a Button
Web Colors in an Android Color Xml Resource File
Get Referrer After Installing App from Android Market
Android Popup Window Dismissal
Play Audio File from the Assets Directory
Calling Startactivity() from Outside of an Activity Context
Error:(6, 0) Gradle Dsl Method Not Found: 'Google()'
Using the Android Recognizerintent with a Bluetooth Headset
How to Add a Button to Preferencescreen