天天看点

You must call removeView() on the child's parent first

首先,android中的fragment是什么?

http://developer.android.com/guide/topics/fundamentals/fragments.html

场景:

现有两个fragment(e.g:loginfragment、homefragment)需要在android程序运行的时候进行动态加载、切换,这种情况下,比较容器出现的一个问题就是:

这里,贴出处理此问题的几个代码片段,备忘之:

main.xml

    出现the specified child already has a parent. you must call removeview() on the child's parent first.这个问题,一般原因是对layout.xml的使用理解不清楚。

    以xml文件方式来设计界面的布局,如果需要动态的对xml文件中的各类view进行修改的话,在代码中使用时,不能直接使用this.findviewbyid(r.id.***)来获取xml文件中的每个view,然后再将这些view加入到代码中的layout中来进行显示。正确的做法应该是使用inflater。

    举例如下:

xml布局文件test.xml为:

1

You must call removeView() on the child's parent first

<?xml version="1.0" encoding="utf-8"?>

 2

You must call removeView() on the child's parent first

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"

 3

You must call removeView() on the child's parent first

    android:orientation="vertical" android:layout_width="fill_parent"

 4

You must call removeView() on the child's parent first

    android:layout_height="fill_parent">

 5

You must call removeView() on the child's parent first

 6

You must call removeView() on the child's parent first

    <textview android:id="@+id/tv1" android:layout_gravity="center_vertical"

 7

You must call removeView() on the child's parent first

        android:layout_width="wrap_content" android:layout_height="wrap_content"

 8

You must call removeView() on the child's parent first

 />

 9

You must call removeView() on the child's parent first

10

You must call removeView() on the child's parent first

    <textview android:id="@+id/tv2" android:layout_gravity="center_vertical"

11

You must call removeView() on the child's parent first

12

You must call removeView() on the child's parent first

         />

13

You must call removeView() on the child's parent first

14

You must call removeView() on the child's parent first

    <textview android:id="@+id/tv3"

15

You must call removeView() on the child's parent first

        android:layout_gravity="center_vertical" android:layout_width="wrap_content"

16

You must call removeView() on the child's parent first

        android:layout_height="wrap_content"  />

17

You must call removeView() on the child's parent first

18

You must call removeView() on the child's parent first

    <imageview android:src="@drawable/line" android:layout_width="fill_parent"

19

You must call removeView() on the child's parent first

        android:layout_height="fill_parent"  />

20

You must call removeView() on the child's parent first

21

You must call removeView() on the child's parent first

</linearlayout>

如果你需要使用这个布局xml文件,并根据自己的需要,将其中三个textview的文字做更改,则在代码中应该这样去使用:

 1

You must call removeView() on the child's parent first
You must call removeView() on the child's parent first
You must call removeView() on the child's parent first
You must call removeView() on the child's parent first

     layoutinflater inflate = (layoutinflater)

You must call removeView() on the child's parent first

         getsystemservice(context.layout_inflater_service);

You must call removeView() on the child's parent first

         linearlayout layout = (linearlayout)inflate.inflate(r.layout.poemshowlist, null); 

You must call removeView() on the child's parent first
You must call removeView() on the child's parent first

         ((textview)layout.findviewbyid(r.id.tv1)).settext(text1);

You must call removeView() on the child's parent first

         ((textview)layout.findviewbyid(r.id.tv2)).settext(text2);

You must call removeView() on the child's parent first

         ((textview)layout.findviewbyid(r.id.tv3)).settext(text3);

You must call removeView() on the child's parent first
You must call removeView() on the child's parent first

         linearlayout ll= new linearlayout(this);

You must call removeView() on the child's parent first

         ll.addview(layout);

You must call removeView() on the child's parent first

         setcontentview(ll);

You must call removeView() on the child's parent first
You must call removeView() on the child's parent first
You must call removeView() on the child's parent first
You must call removeView() on the child's parent first
You must call removeView() on the child's parent first

继续阅读