码迷,mamicode.com
首页 > 移动开发 > 详细

Android-The specified child already has a parent. You must call removeView() on the child's parent first.

时间:2015-02-13 06:55:53      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

  这个问题搞了我半天了,网上有很多人说需要找到该控件的parent后,让该parent 先remove需要添加的控件,然后在添加,如:

if (view != null) {
    ViewGroup parent = (ViewGroup) view.getParent();
    if (parent != null) {
        parent.removeView(view);
    }
}
try {
    view = inflater.inflate(R.layout.fragment_main, container, false);
} catch (InflateException e) {

}

 也很多人说将inflater.inflate(R.layout.change_layout)改为 inflater.inflate(R.layout.change_layout, container, false);

但是我的情况是这样的,有一个FragmentManager,控制4个Fragment,其中,3个Fragment是直接从layout文件获取,所以直接调用inflater.inflate(R.layout.change_layout, container, false);就可以,但是另一个Fragment是动态从我的自定义的View类加载,我一开始总是使用addView,不管我怎么通过父类去remove,都报一样的错.

后来发现,在Fragment的onCreateView方法中,不用显示addView方法,只要实例化一个View对象,即可(甚至不用返回该View对象).如

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		frameLayout = (FrameLayout) getActivity().findViewById(R.id.content);
		final String title = "最近一周AQI变化图";
		data = new int[] { 1, 2, 3, 4, 5, 6, 7 };
		xLables = new String[] { "一", "二", "三", "四", "五", "六", "七" };

		view_brokenline = new BrokenLineView(frameLayout.getContext(), title,
				xLables, null, data);
		return null;
	}

 

Android-The specified child already has a parent. You must call removeView() on the child's parent first.

标签:

原文地址:http://www.cnblogs.com/listened/p/4289563.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!