ViewGroup.java AddViewInner问题 [英] ViewGroup.java AddViewInner issue

查看:365
本文介绍了ViewGroup.java AddViewInner问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在追随错误,基本上是相同的错误,但是总是在不同的行上发生

i keep getting following errors, it's basically same error but keeps occuring on different lines

致命异常:java.lang.IllegalStateException:指定的子代已经有一个父代.您必须先在孩子的父母上调用removeView(). 在android.view.ViewGroup.addViewInner(ViewGroup.java:3883)

Fatal Exception: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. at android.view.ViewGroup.addViewInner(ViewGroup.java:3883)

致命异常:java.lang.IllegalStateException:指定的子代已经有一个父代.您必须先在孩子的父母上调用removeView(). 在android.view.ViewGroup.addViewInner(ViewGroup.java:4312)

Fatal Exception: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. at android.view.ViewGroup.addViewInner(ViewGroup.java:4312)

致命异常:java.lang.IllegalStateException:指定的子代已经有一个父代.您必须先在孩子的父母上调用removeView(). 在android.view.ViewGroup.addViewInner(ViewGroup.java:4310)

Fatal Exception: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. at android.view.ViewGroup.addViewInner(ViewGroup.java:4310)

我尝试过的事情:-

  • 在添加任何片段的同时,我将此代码添加到onCreateView

  • while adding any fragment i add this code in onCreateView

((ViewGroup)view.getParent()).removeView(view); view = inflater.inflate(R.layout.create_carpool_layout,container,false); 返回视图;

((ViewGroup) view.getParent()).removeView(view); view = inflater.inflate(R.layout.create_carpool_layout, container, false); return view;

然后扩大视图

在适配器中扩大布局/视图的同时,我还确保将视图从父视图中移除

While inflating layouts / views in adapter also i make sure the view is removed from parent view

我不确定是什么原因导致了此问题

I am not sure what is causing this problem

推荐答案

尝试仔细检查一下如何放大视图.

Try double checking how you are inflating the view.

有两种可能的方法:

  1. 将父项传递给充气机:

  1. Passing the parent to the inflater:

LayoutInflater.from(context).inflate(R.layout.layout, this, true);

  • 保留没有父视图的视图:

  • Keep the view without the parent:

    LayoutInflater.from(context).inflate(R.layout.layout, null);
    

  • 使用第一种方法时,不能使用view作为参数调用addView().
    使用第二种方法,视图未固定到父视图,因此您可以调用addView()

    When using the first method, you cannot call addView() with the view as parameter.
    Using the second way, the view is not pinned to parent, therefore you can invoke addView()

    这篇关于ViewGroup.java AddViewInner问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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