【发布时间】:2015-08-06 20:20:27
【问题描述】:
我不确定为什么会发生这种情况,但我收到一条错误消息:调用 layout.removeAllViews(); 仍会导致 IllegalStateException: The specified child already has a parent. 您必须先在孩子的父母上调用 removeView()。
奇怪的部分是我调用的:removeAllViews();在添加新之前:
@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);
但我仍然收到致命错误...所以我不确定为什么会发生这种情况。
感谢任何建议。
【问题讨论】:
-
请发布您的整个堆栈跟踪。我猜你的问题不在于
layout,而在于imageViewz。 -
谢谢@CommonsWare - 如果这对你来说足够详细,请告诉我 - pastebin.com/4uWdz7Zq(顺便说一句,第 154 行是 layout.addView(imageViewz);)
标签: java android android-linearlayout fatal-error illegalstateexception