本文介紹了指定的孩子已經有一個父母.您必須首先在孩子的父母上調用 removeView() (Android)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我必須經常在兩種布局之間切換.錯誤發生在下面發布的布局中.
I have to switch between two layouts frequently. The error is happening in the layout posted below.
當我的布局第一次被調用時,沒有發生任何錯誤,一切都很好.然后當我調用不同的布局(空白的)然后再次調用我的布局時,它會引發以下錯誤:
When my layout is called the first time, there doesn't occur any error and everything's fine. When I then call a different layout (a blank one) and afterwards call my layout a second time, it throws the following error:
> FATAL EXCEPTION: main
> java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
我的布局代碼如下所示:
My layout-code looks like this:
tv = new TextView(getApplicationContext()); // are initialized somewhere else
et = new EditText(getApplicationContext()); // in the code
private void ConsoleWindow(){
runOnUiThread(new Runnable(){
@Override
public void run(){
// MY LAYOUT:
setContentView(R.layout.activity_console);
// LINEAR LAYOUT
LinearLayout layout=new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
setContentView(layout);
// TEXTVIEW
layout.addView(tv); // <========== ERROR IN THIS LINE DURING 2ND RUN
// EDITTEXT
et.setHint("Enter Command");
layout.addView(et);
}
}
}
我知道以前有人問過這個問題,但對我的情況沒有幫助.
I know this question has been asked before, but it didn't help in my case.
推薦答案
錯誤信息說明了你應該做什么.
The error message says what You should do.
// TEXTVIEW
if(tv.getParent() != null) {
((ViewGroup)tv.getParent()).removeView(tv); // <- fix
}
layout.addView(tv); // <========== ERROR IN THIS LINE DURING 2ND RUN
// EDITTEXT
這篇關于指定的孩子已經有一個父母.您必須首先在孩子的父母上調用 removeView() (Android)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!