您的位置:首页 > 其它

有关于子控件填充容器的异常说明

2014-09-01 17:03 253 查看
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

代码如下:在编写fragment的过程中

public class DetailFragment extends Fragment {

BookContent.Book book;

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

// TODO Auto-generated method stub

View root=inflater.inflate(R.layout.fragmentdetail, container);

if(book!=null){

((TextView)root.findViewById(R.id.book_title)).setText(book.title);

System.out.println(book.title);

((TextView)root.findViewById(R.id.book_desc)).setText(book.detail);

}

return root;

}

其中通过View root=inflater.inflate(R.layout.fragmentdetail, container);获得view为子控件,不能填充fragment容器需要解除与父空间的关系,所以要调用inflate的

View root=inflater.inflate(R.layout.fragmentdetail, container,false);//第三个参数是否解除与父控件之间的联系
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: