我们通常使用inflate(int resource, ViewGroup root, boolean attachToRoot)方法来加载fragment的布局,在这种情况下会默认添加到母布局,即attachToRoot默认为true。这就会导致异常:java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.(原因:由于当前的view已被添加到fragment中,这时再将它添加到母布局中必然会使其有多个parent)
所以在使用该方法时一定要手动将最后一个参数设置为false!