调用removeAllViews();仍然导致IllegalStateException异常:必须首先对孩子的父母打电话removeView() [英] Calling removeAllViews(); still results in IllegalStateException:You must call removeView() on the child's parent first
问题描述
我不知道为什么发生这种情况,但我得到一个错误,说明:调用 layout.removeAllViews();
仍然导致 IllegalStateException异常:该指定的小孩已经有一个父
您必须在孩子的父母先调用removeView()
I'm not sure why this is happening but I'm getting an error stating: Calling layout.removeAllViews();
still results in IllegalStateException: The specified child already has a parent.
You must call removeView() on the child's parent first.
但奇怪的是我打过电话:removeAllViews();添加一个新的人之前:
The strange part is I've called: removeAllViews(); before adding a new one:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.download);
...
ImageView imageViewz = (ImageView) findViewById(R.id.imageView6);
Picasso.with(context).load(background).into(imageViewz);
LinearLayout layout = new LinearLayout(Download.this);
layout.setId(R.id.download);
LayoutParams layoutParams
= new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layout.setLayoutParams(layoutParams);
layout.setOrientation(LinearLayout.VERTICAL);
LayoutParams imageViewLayoutParams
= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imageViewz.setLayoutParams(imageViewLayoutParams);
layout.removeAllViews();
layout.addView(imageViewz);
setContentView(layout);
不过,我仍然得到了致命的错误...所以我不知道究竟为什么发生这种情况。
Yet I still get the fatal error...so I'm not sure exactly why this is happening.
任何建议都AP preciated。
Any suggestions are appreciated.
推荐答案
您的问题是不是与布局
。你的问题是与 imageViewz
。它已经拥有了父母,这是什么触发你的异常。你需要从当前父删除 imageViewz
将其添加到布局
。
Your problem is not with layout
. Your problem is with imageViewz
. It already has a parent, and that is what is triggering your exception. You need to remove imageViewz
from its current parent before you add it to layout
.
这篇关于调用removeAllViews();仍然导致IllegalStateException异常:必须首先对孩子的父母打电话removeView()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!