gpt4 book ai didi

android - IllegalStateException : The Specified child already has a parent. 您必须调用 removeView()

转载 作者:行者123 更新时间:2023-11-29 15:47:36 25 4
gpt4 key购买 nike

大家好,首先抱歉我的英语不好(我不是母语人士),我知道这个问题之前已经被问过好几次了。由于解决方案非常个性化,我有点困惑。

问题是:我正在尝试向 LinearLayout 添加多个 View (TextView、View)。添加前两个组件工作正常,但第三个 View 出现异常。这是我的代码

 if(textView_value != null) {
textView_value = null;
}

textView_value = new TextView(this);
textView_value.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView_value.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

linearLayout.addView(textView_value);

View view = new View(this);
view.setLayoutParams(...);
...
linearLayout.addView(view);

textView_value = new TextView(this);
...

linearLayout.addView(textView_value); // Here is the app crashing :(

最佳答案

您不能多次添加相同的 View 。如果要添加相同的引用,首先需要将其从 View 组中删除并添加。所以改变这两行

textView_value = new TextView(this);
linearLayout.addView(textView_value);

作为

TextView textView_value1 = new TextView(this);
linearLayout.addView(textView_value1);

关于android - IllegalStateException : The Specified child already has a parent. 您必须调用 removeView(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32138095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com