java.lang.IllegalStateException

Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.


出错代码

ImageView imageView = new ImageView(this);

for(int i=0;i<4;i++){
            for (int a=0;a<4;a++){
                imageView.setImageResource(imagePath[(int)Math.random()*3]);
                BlackBricksVector.add(imageView);
                tableRows[i].addView(imageView);
             
            }
        }
 
 

解决方法

用另外创建的ImageView对象替换出错行中的imageView,没有报错,说明问题出现在出错行中的imageView上

for(int i=0;i<4;i++){
            for (int a=0;a<4;a++){
                imageView = new ImageView(this);
                imageView.setImageResource(imagePath[(int)Math.random()*3]);
                BlackBricksVector.add(imageView);
                tableRows[i].addView(imageView);
                
            }
        }

末尾处加上imageView=null;语句

并在每一次循环时重新new ImageView对象











猜你喜欢

转载自blog.csdn.net/yyyerica/article/details/50899934