作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
大家好,首先抱歉我的英语不好(我不是母语人士),我知道这个问题之前已经被问过好几次了。由于解决方案非常个性化,我有点困惑。
问题是:我正在尝试向 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/
我是一名优秀的程序员,十分优秀!