【发布时间】:2014-06-08 19:49:23
【问题描述】:
我正在回答我自己的问题,因为即使我想通了,我也无法得到明确的解决方案。这可能对其他人有所帮助。
我知道我收到的异常很常见,但我无法通过以下页面解决我的问题:
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/
我想做的是将一堆TextViews 添加到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 是我想要添加TextViews 的那个,它的id 是postsView。
所以我要创建一个LinearLayout,将TextViews 添加到该布局,然后将该布局添加到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 中删除视图?
【问题讨论】:
-
所以你只是想在xml中添加滚动视图,就这样吗??
-
@DushyantPatel 是的。困难的原因是因为我将创建的布局添加到 two ScrollViews(用于测试目的),所以我遇到了意外错误。然后我发现唯一真正的问题是我添加了太多视图而不是每次都删除它们。问题解决了,我回答了我自己的问题。
标签: android xml scrollview