【问题标题】:Fragment- unable to add child view, says child already has parent片段 - 无法添加子视图,表示子视图已经有父视图
【发布时间】:2018-12-01 07:54:45
【问题描述】:

在片段中:我正在尝试添加视图但收到错误 Exception IllegalStateException 。

我尝试过以下解决方案:

1) solution one

2) Call removeView() on the child's parent first

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

在尝试了上述所有解决方案后仍然出现同样的错误。

这是我的代码:

片段.java:

 View kidInfoView = getLayoutInflater().inflate(R.layout.include_kid_info_layout,null,false);

        if (onCollapsingToolbarLayout().getParent() !=null) {
           ViewGroup parent = (ViewGroup) onCollapsingToolbarLayout().getParent();
           parent.removeView(kidInfoView);
            onCollapsingToolbarLayout().addView(kidInfoView); <-- getting error here
        }

@Override
    public CollapsingToolbarLayout onCollapsingToolbarLayout() {
        collapsingToolbarLayout.setVisibility(View.VISIBLE);
        return collapsingToolbarLayout;
    }

我做错了什么?请提出建议。

【问题讨论】:

  • 你能发布onCollapsingToolbarLayout() 方法的作用吗?
  • @Aaron 它只是返回 CollapsingToolbarLayout
  • 它会膨胀任何东西吗?或者只是找到视图?
  • @Aaron 添加了onCollapsingToolbarLayout() 方法

标签: android android-collapsingtoolbarlayout viewgroup


【解决方案1】:

如果你想在折叠工具栏中添加一些视图,你需要编写如下代码。
View idInfoView = getLayoutInflater().inflate(R.layout.include_kid_info_layout, onCollapsingToolbarLayout(), true);

我不知道你在 if 条件下在做什么。

您正在获取 collepsingtoolbar 的父级,并且您正在从该父级调用 KidInfoView 上的删除视图(作为第一个子级)(这是夸大了上面的两个语句并且未添加到父级)。
之后,您将 KidInfoView 添加到 collapsingtoolbar(但 kindInfoView 没有匹配的布局参数。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多