Android Can';t删除使用包含标记包含的视图

Android Can';t删除使用包含标记包含的视图,android,Android,删除使用标记包含的视图似乎没有效果 ViewGroup currentPersonContainer = (ViewGroup) root.findViewById(R.id.propria_pessoa_container); if (dto.occupation!= null) { . . . } else { root.removeView(currentPersonContainer); } 在root.remove

删除使用
标记包含的视图似乎没有效果

ViewGroup currentPersonContainer = (ViewGroup) root.findViewById(R.id.propria_pessoa_container);
if (dto.occupation!= null) {
    .
    .
    .                   
} else {
    root.removeView(currentPersonContainer);
}
root.removeView(currentPersonContainer)之后我仍然可以在根目录中找到它,并且视图仍然可见


如何删除xml布局资源文件中包含的视图?

您可以通过调用以下命令删除此视图:

((ViewGroup)(currentPersonContainer.getParent()).removeView(currentPersonContainer);
这将从其直接父视图中删除视图。

使用
标记时,必须重新分配
id

例如:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_width="match_parent" >

    <include 
        layout="@layout/layout_to_include"
        android:id="@+id/some_id" />

</RelativeLayout>

现在可以通过
some\u id
访问包含的视图。您可以通过检查当前设置中,
currentPersonContainer
是否初始化为
null
来验证这一点。我想是的。当
View
为空时,
removeView(View)
会自动打开


简言之,不管您的实际布局是否在根元素上设置了id,您仍然需要在
标记内分配一个
id

currentPersonContainer
的直接子元素吗?不,不是。嗯,这可能就是问题所在。告诉我们currentPersonContainer和root是如何相互关联的?可能是XML文件?它起作用了,我从直接的父对象中删除了它@SimonMarquis您能编辑您的答案以便我接受吗?您希望我如何编辑它?这将导致
NullPointerException
。我能够使用findViewById()方法找到并更新它。我只是没能把它移走。