Java 动态地将TableLayout添加到LinearLayout+从LinearLayout中删除刚刚加载的TableLayout,然后再次加载它,给出错误提示

Java 动态地将TableLayout添加到LinearLayout+从LinearLayout中删除刚刚加载的TableLayout,然后再次加载它,给出错误提示,java,android,android-layout,Java,Android,Android Layout,我有一个关于嵌套表格布局的线性布局的问题。 TableLayout中会动态填充行COL和ImageView。 请参见函数addImagesToTableLayout 事实上,当我启动应用程序时,一切都正常。LinearLayout使用与ImageView嵌套的嵌套TableLayout加载 我想完成的是,当单击其中一个图像时,必须完全删除父TableLayout(包括TableLayout)中的所有图像。 因此,我可以向线性布局添加新的新表格布局 移除不是问题,问题在于调用addImagesTo

我有一个关于嵌套表格布局的线性布局的问题。 TableLayout中会动态填充行COL和ImageView。 请参见函数addImagesToTableLayout

事实上,当我启动应用程序时,一切都正常。LinearLayout使用与ImageView嵌套的嵌套TableLayout加载 我想完成的是,当单击其中一个图像时,必须完全删除父TableLayout(包括TableLayout)中的所有图像。 因此,我可以向线性布局添加新的新表格布局

移除不是问题,问题在于调用addImagesToTableLayout时; 因此,第二次删除后,我收到以下错误消息:

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


 private void addImagesToTableLayout(ArrayList<ImageView> imageViewCollection){

        tablelayout =  new TableLayout(this);
        LayoutParams param = new TableLayout.LayoutParams(
                     LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        param.weight = 2.0f;
        param.gravity = Gravity.CENTER;
        param.leftMargin = portraitmargin - (sqr) ;

        tablelayout.setLayoutParams(param); 
        tableRows = new TableRow[levelNumber-1];

        int m = 0;//modulo
        int i = 0;
        int f = 0;

        for (ImageView s : imageViewCollection)
        {
            m = (i) % sqr;  

            if (m == 0){

                 tableRows[f] = new TableRow(this);  
                 tableRows[f].setId(f+1);
                 f++;     
             } 

             tableRows[f-1].addView(imageViewCollection.get(i));
             i++;
        }

         for(int j = 0; j < sqr; j++) {

        tableRows[j].setLayoutParams(new  LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
                     tableRows[j].setWeightSum(1.0f); 
                     tablelayout.addView(tableRows[j]);
         }

         linearlayout.addView(tablelayout);
 }


// remove tablelayout                   
linearlayout.removeAllViews();

if(linearlayout.getChildCount() == 0) {
  // add a new TableLayout to my root linearLayout (gives error see above)
  addImagesToTableLayout(imageViewCollection);
}

我会让你失望,因为你的头衔和这个问题无关。。。我花了3-4分钟回答标题的问题,只是为了看一看您的代码,看看您是否真正知道如何将表格布局添加到线性布局中。。。这就是你的标题中所要求的。。。学会用你的问题贴上合适的标题。对不起,弗拉德你的权利,但如果你读得好,我想我很清楚,不管怎样我改变了标题,你也是对的。我不好,我看了你的代码表面上。。。无论如何,我想我已经回答了你的问题,希望如此。如果您需要更多详细信息,请通过评论让我知道。谢谢Vlad,我想我的问题不清楚,我的removeViews代码位于我的onClickhandler中。我的setOnClickListener在我声明的代码中的其他地方,但与我的问题无关。我的问题:我想完成的是,当单击其中一个图像时,父TableLayout(包含TableLayout)中的所有图像都将被完全删除。因此,我可以向线性布局添加新的新表格布局。到目前为止,我看到添加和删除都是成功的,但是添加新的tablelaout到linearlaout会给出错误消息。
int count = tablelayout.getChildCount();
for(int i = 0; i < count; i++){
   View v = tablelayout.getChildAt(i);
    if(v instanceof TableRow){
    TableRow row = (TableRow)v;
       int rowCount = row.getChildCount();
         for (int r = 0; r < rowCount; r++){
           View v2 = row.getChildAt(r);
              if (v2 instanceof Button){
               Button b = (Button)v2;
               b.setOnClickListener(new onClickListener ...//add the mehtod yourself please,I always use eclipse's hints,I forgot exactly how the method looks, but it's a method that has a View v as parameter );
        }
    }
}
tablelayout.removeAllViews();