我回答了我自己的问题,因为即使我弄清楚了,我也无法得到明确的解决方案。这可能有助于其他人。
我知道我收到的例外情况很常见,但我无法通过以下页面解决我的问题:
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/
我想要做的是将一堆TextView
添加到ScrollView
,然后将ScrollView
添加到屏幕上。问题是ScrollView
只是屏幕的一部分,因此我无法使用setContentView(scrollView)
,因为这会取代我已经拥有的按钮和内容。
我有一个XML文件,其中包含我的布局模板,我只想将ScrollView
替换为我在Activity中生成的模板。
这是布局:
<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>
底部的ScrollView
是我要添加TextView
的{{1}},其ID为postsView
。
因此,我要创建LinearLayout
,将TextView
添加到该布局,然后将该布局添加到ScrollView
。像这样:
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);
但是有了这个,我得到了LogCat:
java.lang.IllegalStateException: ScrollView can host only one direct child
如何从ScrollView
删除视图?
答案 0 :(得分:0)
您可以在ScrollView
上使用removeAllViews()
。这种情况正在发生,因为无论何时多次运行代码,都会向ScrollView
添加多个视图。
您的代码会改变这一点:
postsView.addView(layout);
要:
postsView.removeAllViews()
postsView.addView(layout);