关于向父view添加相同的view问题!The specified child already has a parent. You must call removeView() on the chil


代码片段,双击复制
01
02
03
04
for ( int i= 0 ; i < 5 ; i++){
   View view = LayoutInflater.from( this ).inflate(R.layout.item_bg, null );
  lin_item.addView(view);
}


lin_item为一个LinearLayout,把view动态加到这个lin_item中,但是只能加进一条数据。。。要不然就报07-28 22:03:49.360: ERROR/AndroidRuntime(1403): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
这个异常,我知道view为同一个,所以会产生这种异常。但是我就是要这样动态的加入view到lin_item里啊。。有没什么办法呢。。

代码片段,双击复制
01
02
03
04
05
06
View view1 = LayoutInflater.from( this ).inflate(R.layout.lin_item_ev, null );
View view2 = LayoutInflater.from( this ).inflate(R.layout.lin_item_ev, null );
View view3 = LayoutInflater.from( this ).inflate(R.layout.lin_item_ev, null );
lin_item.addView(view1);
lin_item.addView(view2);
lin_item.addView(view3);

如果这样写就没问题了。。但是还是同一个layout啊。。。我知道原因的,但有没办法可以像上面那样动态的生成view并加到linearLayout里。。。


以上同样情况。。同一个view不能让父view  addview 2次,所以解决办法是:

LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
        
        dots = new ImageView[imageColors1.length];
        // 循环取得小点图片
        for (int i = 0; i < imageColors1.length; i++) {
            dots[i] =  (ImageView) LayoutInflater.from(this).inflate(R.layout.dot, null);
            dots[i].setEnabled(true);// 都设为灰色
            ll.addView(dots[i]);
        }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值