gpt4 book ai didi

android - 冲突的 Android 错误消息 : The specified child already has a parent. 您必须先对 child 的 parent 调用 removeView()

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:30:39 66 4
gpt4 key购买 nike

最初我得到这个错误:

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

如果 child 有 parent ,我必须先从 parent 那里删除 child ,为什么 getParent() 返回 null?

我有一个抽象 fragment ,允许派生类为列表适配器提供自定义布局。相关代码:

绑定(bind):

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, null) ... false, null/false, 什么给了?

编辑:

@allprog,我知道需要进行一些清理。我在一天结束时写了这篇文章,有点匆忙。从那以后,我清理了代码,并分离出 View 的绑定(bind)和膨胀。清理代码:

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 首先得到它,用我原来的 [undempt] 代码就是解决方案。

最佳答案

试试这个:

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);
}

}

我已经阅读了相关的源代码,当view 有parent 时,getParent 应该返回非空值。在转换和调用 removeView

之前,您应该 确保它确实有父级

希望这对您有所帮助。

源代码:

View 中:

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.");
}

关于android - 冲突的 Android 错误消息 : The specified child already has a parent. 您必须先对 child 的 parent 调用 removeView(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21642016/

66 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com