I'm answering my own question because even though I figured it out, I wasn't able to get a clear solution. This may help someone else.
I know the exception I receive is a common one, but I couldn't solve my problem with the following pages:
The specified child already has a parent. You must call removeView() on the child's parent first
java.lang.IllegalStateException: The specified child already has a parent
Call removeView() on the child's parent first
http://www.phonesdevelopers.com/1716777/
What I want to do it add a bunch of TextView
s to a ScrollView
, and then put the ScrollView
on the screen. The problem is that the ScrollView
is only part of the screen, so I can't just use setContentView(scrollView)
because that would replace the buttons and things I already have.
I have an XML file that contains the template for my layout, and I simply want to replace the ScrollView
with the one I generated in my Activity.
Here is the layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mostParent" >
<RelativeLayout
android:id="@+id/loadingPanel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true" />
</RelativeLayout>
<Button
android:id="@+id/btnLogout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/logout" />
<ImageButton
android:id="@+id/refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_action_refresh" />
<ToggleButton
android:id="@+id/lockToggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text=""
android:textOff="@string/toggle_off"
android:textOn="@string/toggle_on" />
<TextView
android:id="@+id/loggedInAs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/refresh"
android:layout_centerHorizontal="true"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
<ScrollView
android:id="@+id/postsView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/btnLogout"
android:layout_marginTop="30dp"
android:scrollbarSize="3dip" />
</RelativeLayout>
The ScrollView
at the bottom is the one I want to add the TextView
s to and it has the id postsView
.
So I'm going to create a LinearLayout
, add the TextView
s to that layout, and then add that layout to the ScrollView
. Like this:
ScrollView postsView = (ScrollView)findViewById(R.id.postsView);
LinearLayout layout = new LinearLayout(MyActivity.this);
layout.setOrientation(LinearLayout.VERTICAL);
for (final TextView post : posts) {
// add post to layout
layout.addView(post);
}
postsView.addView(layout);
But with this, I get the LogCat:
java.lang.IllegalStateException: ScrollView can host only one direct child
How can I remove the view from the ScrollView
?