【问题标题】:Exception thrown: You must call removeView on fragment create抛出异常:您必须在片段创建时调用 removeView
【发布时间】:2014-04-15 08:27:53
【问题描述】:

我得到“指定的孩子已经有一个父母。你必须先在孩子的父母上调用 removeView()。”在调用我的 onCreateView 方法后直接抛出。

InkomendFragment

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_inkomend,
                container);
        return v;
    }

【问题讨论】:

  • 你也可以这样做:View v = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_inkomend, container , false);

标签: android android-fragments android-lifecycle oncreate layout-inflater


【解决方案1】:

我花了很长时间才发现

正确的语法:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_inkomend,
            null);
    return v;
}

显然您不需要在参数“root”中添加任何内容。

【讨论】:

    【解决方案2】:

    试试这个..

    以下也是正确的语法

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

    【讨论】:

    • 你是对的。什么会更好?好像不需要容器ViewGroup吧?
    • @BartBurg 是的,与onCreateView 构造函数中的LayoutInflater 中的inflater 相同。无需外部充气。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 2020-05-07
    • 2020-05-12
    • 2015-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多