0
mContainer.removeAllViewsInLayout();
mContainer.addView(nViews)
但我recive錯誤:
The specified child already has a parent. You must call removeView()
on the child's parent first
有什麼不好?
mContainer.removeAllViewsInLayout();
mContainer.addView(nViews)
但我recive錯誤:
The specified child already has a parent. You must call removeView()
on the child's parent first
有什麼不好?
有removeAllViewsInLayout()
和removeAllViews()
之間的一個重要區別。
前者可能不會刪除所有視圖,後者會刪除所有視圖。除非您正在自己的邏輯中確定在removeAllViewsInLayout()
中刪除了哪些視圖,否則您可能會嘗試將重複視圖添加到佈局中。
由於事實上,removeAllViewsInLayout()
配備了這樣的警告:
不要調用此方法,除非你正在擴展的ViewGroup和理解角度測量和佈局的管道。
根據您的文章,我假設您沒有擴展ViewGroup並正確分析測量/佈局管道,因此不應該調用該方法。
但是,如果mContainer
不是nViews
的實際父項,則清除mContainer
將是不夠的。在這種情況下,您必須先使用removeView()
方法刪除nViews
,然後才能將其添加到mContainer
。
removeAllViews()=>相同的錯誤; mContainer是我的layout.xml的根LiniarLayout – Corneliu
'nViews'從哪裏來?你還沒有發佈足夠的信息來分析爲什麼'nViews'會被添加兩次,但是錯誤信息非常清楚,就是發生了什麼。 –