本文介紹了調用emoveAllViews();仍會導致IlLegalStateException:您必須首先對子對象的父級調用emoveView()的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我不確定為什么會發生這種情況,但我收到錯誤消息:調用layout.removeAllViews();
仍然會導致IllegalStateException: The specified child already has a parent.
您必須首先在子級的父級上調用emoveView()。
奇怪的是,我在添加新的之前調用了:emoveAllViews():
@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
。它已經有了父級,這就是觸發您的異常的原因。在將layout
添加到layout
之前,您需要從其當前父級中刪除imageViewz
。
這篇關于調用emoveAllViews();仍會導致IlLegalStateException:您必須首先對子對象的父級調用emoveView()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,