冲突的Andr​​oid的错误消息:指定的孩子已经有一个父。你必须先调用removeView()对孩子的父 [英] Conflicting Android error messages: The specified child already has a parent. You must call removeView() on the child's parent first

查看:241
本文介绍了冲突的Andr​​oid的错误消息:指定的孩子已经有一个父。你必须先调用removeView()对孩子的父的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本来我得到这个错误:

指定的孩子已经有一个父。必须调用removeView()   孩子的父母第一次

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

customSection.addView(customLayout);

所以我添加

((LinearLayout)customLayout.getParent()).removeView(customLayout);

和现在得到

java.lang.NullPointerException

因此,如果孩子有父母,我必须首先从父,取出孩子为什么的getParent()返回NULL?

我有一个抽象的片段,使派生类来提供定制的布局列表中的适配器。相关code:

I have an abstract fragment that allows derived classes to supply a custom layout for the list adapter. Relevant code:

绑定:

public void bind(DataObject row) {
    View customLayout = getChildItemView(row);
    if (customLayout != null) {
        ((LinearLayout) customLayout.getParent()).removeView(customLayout);
        customSection.removeAllViews();
        customSection.addView(customLayout);
        customSection.setVisibility(View.VISIBLE);
    } else {
        customLayout.setVisibility(View.INVISIBLE);
    }

}

protected View getChildItemView(CommonRow row) {
    if (parentView == null) {
        parentView = (LinearLayout) LayoutInflater.from(getActivity())
                .inflate(R.layout.list_item_custom_section,
                        new LinearLayout(getActivity()), true);
        label = (TextView) parentView.findViewById(R.id.txtData1Label);
        value = (TextView) parentView.findViewById(R.id.txtData1Value);
    }
    label.setText("Minimum");
    value.setText(manager.formatMoney(((SpecificDataRow) row).minimum));
    return parentView;
}

我也试过 inflater.inflate(R.layout.list_item_custom_section,空) ...假的,空/假的,怎么办?

I've also tried inflater.inflate(R.layout.list_item_custom_section, null) ... false, null / false, what gives?

编辑:

@allprog,我知道是需要进行一些清理。我写这在这一天有些匆忙结束。因为我已经清理了code,分离出视图的约束力和膨胀。清理code:

@allprog, I knew some cleanup was needed. I wrote this at the end of the day somewhat in a hurry. I have since cleaned up the code, and separated out the binding and inflating of the view. Cleaned up code:

private class ViewHolder {
....

       public ViewHolder(View v) {
            Butterknife.inject(this, v);
            View custom = createCustomView(customSection);
            if (custom != null) {
                customSection.setVisibility(View.VISIBLE);
                customSection.addView(custom);
            }
        }

        public void bind(CommonRow row) {
            ......

            bindCustomView(row, customSection);
        }

}

子类:

    @Override
    protected View createCustomView(ViewGroup parent) {
        return LayoutInflater.from(getActivity()).inflate(R.layout.list_item_custom_section, parent, false);
    }


    @Override
    protected void bindCustomView(CommonRow row, ViewGroup section) {
        TextView label = Views.findById(section, R.id.txtData1Label);
        TextView value = Views.findById(section, R.id.txtData1Value);

        label.setText("Minimum");
        value.setText(manager.formatMoney(((SpecificRow) row).minimum));
    }

suitianshi得到它首先,与我原来的[蓬头垢面] code,这是解决方案。

suitianshi got it first, with my original [unkempt] code that was the solution.

推荐答案

试试这个:

public void bind(DataObject row) {
    View customLayout = getChildItemView(row);
    if (customLayout != null) {
         if(customLayout.getParent() != null) {
             ((LinearLayout)customLayout.getParent()).removeView(customLayout);
         }

         customSection.removeAllViews();
         customSection.addView(customLayout);
         customSection.setVisibility(View.VISIBLE);
    } else {
         customLayout.setVisibility(View.INVISIBLE);
    }

}

我看了相关的源码code,的getParent 应该返回非空值时,视图有父母。您应该确保它实际上有浇铸和调用之前父 removeView

I have read related source code, getParent should return non-null value when view has a parent. You should make sure it actually has a parent before casting and calling removeView

祝这会有所帮助。

源$ C ​​$ C:

source code :

查看

public final ViewParent getParent() {
        return mParent;
    }

ViewGroup.addViewInner

if (child.getParent() != null) {
     throw new IllegalStateException("The specified child already has a parent. " +
         "You must call removeView() on the child's parent first.");
}

这篇关于冲突的Andr​​oid的错误消息:指定的孩子已经有一个父。你必须先调用removeView()对孩子的父的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆