天天看点

替换Fragment 报错 The specified child already has a parent. You must call removeView() on the child‘s pa

在将一个fragment替换到一个frameLayout的时候报错:

code: transaction.replace(R.id.fragment_container, fragment2);

错误: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child’s parent first.

原因:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
   //View newsView = inflater.inflate(R.layout.fragment_news, container);
        View newsView = inflater.inflate(R.layout.fragment_news, container,false);
        return newsView;
    }