Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:您必须先对孩子的父对象2调用removeView()_Android_Parent Child - Fatal编程技术网

Android:您必须先对孩子的父对象2调用removeView()

Android:您必须先对孩子的父对象2调用removeView(),android,parent-child,Android,Parent Child,我在Android中创建一个动态添加行的表时遇到了一个问题。错误消息是: 指定的子级已具有父级。您必须调用removeView 先看孩子的父母 但是为什么呢 void setCalendario(List<ArrayList<String>> l){ TableLayout table = (TableLayout)findViewById(R.id.list_tableLayout1); TableRow tr = new TableRow(th

我在Android中创建一个动态添加行的表时遇到了一个问题。错误消息是:

指定的子级已具有父级。您必须调用removeView 先看孩子的父母

但是为什么呢

    void setCalendario(List<ArrayList<String>> l){
    TableLayout table = (TableLayout)findViewById(R.id.list_tableLayout1);
    TableRow tr = new TableRow(this);
    tr.removeAllViews();
    tr.setPadding(0,10,0,10);
    tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    TextView tv_item1 = new TextView(this);
    TextView tv_item2 = new TextView(this);
    TextView tv_item3 = new TextView(this);
    for (ArrayList<String> al : l){
        int i = 0;
        for(String s : al){
            if (i == 0){
                i++;
                tv_item1.setText(s);
                tv_item1.setGravity(Gravity.CENTER);
            }
            if (i == 1){
                tv_item2.setText(s);
                tv_item2.setGravity(Gravity.CENTER);
                i++;
            }
            if (i == 2){
                tv_item3.setText(s);
                tv_item3.setGravity(Gravity.CENTER);
                tr.addView(tv_item1);
                tr.addView(tv_item2);
                tr.addView(tv_item3);
                table.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            }
        }
    }

    }
xml代码:

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.MSca.gorhinos.Calendario$PlaceholderFragment" >

    <TableLayout
    android:id="@+id/list_tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:stretchColumns="0,1,2,3" >

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

<TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textColor="#000000"/>

<TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textColor="#000000"/>

<TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textColor="#000000"/>


</TableRow>
</TableLayout>

</RelativeLayout>

您正在使用for循环将对TextView的相同引用添加到TableRow。因此,在循环的下一次迭代中,相同的对象再次添加到TableRow或TableLayout!那时他们已经有了父母

尝试初始化outerfor循环中的TableRow和TextView对象

编辑:修改您的代码

void setCalendario(List<ArrayList<String>> l) {
    // Here we initialize the objects we re-initialize every iteration of the loop
    TableLayout table = (TableLayout)findViewById(R.id.list_tableLayout1);
    for (ArrayList<String> al : l) {
        TableRow tr = new TableRow(this);
    // I can't believe a freshly initialized TableRow object has views attached...
        tr.removeAllViews();
        tr.setPadding(0,10,0,10);
    // Not sure why these layout params are needed already, as they are specified
    // when adding this TableRow to the TableLayout object as well.
        tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        TextView tv_item1 = new TextView(this);
        TextView tv_item2 = new TextView(this);
        TextView tv_item3 = new TextView(this);
        int i = 0;
        for(String s : al) {
            if (i == 0) {
                i++;
                tv_item1.setText(s);
                tv_item1.setGravity(Gravity.CENTER);
            }
            if (i == 1) {
                tv_item2.setText(s);
                tv_item2.setGravity(Gravity.CENTER);
                i++;
            }
            if (i == 2) {
                tv_item3.setText(s);
                tv_item3.setGravity(Gravity.CENTER);
                tr.addView(tv_item1);
                tr.addView(tv_item2);
                tr.addView(tv_item3);
                table.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            }
        }
    }
    } 

您正在使用for循环将对TextView的相同引用添加到TableRow。因此,在循环的下一次迭代中,相同的对象再次添加到TableRow或TableLayout!那时他们已经有了父母

尝试初始化outerfor循环中的TableRow和TextView对象

编辑:修改您的代码

void setCalendario(List<ArrayList<String>> l) {
    // Here we initialize the objects we re-initialize every iteration of the loop
    TableLayout table = (TableLayout)findViewById(R.id.list_tableLayout1);
    for (ArrayList<String> al : l) {
        TableRow tr = new TableRow(this);
    // I can't believe a freshly initialized TableRow object has views attached...
        tr.removeAllViews();
        tr.setPadding(0,10,0,10);
    // Not sure why these layout params are needed already, as they are specified
    // when adding this TableRow to the TableLayout object as well.
        tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        TextView tv_item1 = new TextView(this);
        TextView tv_item2 = new TextView(this);
        TextView tv_item3 = new TextView(this);
        int i = 0;
        for(String s : al) {
            if (i == 0) {
                i++;
                tv_item1.setText(s);
                tv_item1.setGravity(Gravity.CENTER);
            }
            if (i == 1) {
                tv_item2.setText(s);
                tv_item2.setGravity(Gravity.CENTER);
                i++;
            }
            if (i == 2) {
                tv_item3.setText(s);
                tv_item3.setGravity(Gravity.CENTER);
                tr.addView(tv_item1);
                tr.addView(tv_item2);
                tr.addView(tv_item3);
                table.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            }
        }
    }
    } 

因此,您可以创建tv_项目1、tv_项目2和tv_项目3。然后在循环中为所有ArrayList添加此视图

tr.addView(tv_item1);
tr.addView(tv_item2);
tr.addView(tv_item3);
在第二次迭代中,您已经将tv_item1添加到tr中,并且希望再次添加。我想你只需要把这些线转移到自行车上:

TableRow tr = new TableRow(this);
tr.removeAllViews();
tr.setPadding(0,10,0,10);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
TextView tv_item1 = new TextView(this);
TextView tv_item2 = new TextView(this);
TextView tv_item3 = new TextView(this);

因此,您可以创建tv_项目1、tv_项目2和tv_项目3。然后在循环中为所有ArrayList添加此视图

tr.addView(tv_item1);
tr.addView(tv_item2);
tr.addView(tv_item3);
在第二次迭代中,您已经将tv_item1添加到tr中,并且希望再次添加。我想你只需要把这些线转移到自行车上:

TableRow tr = new TableRow(this);
tr.removeAllViews();
tr.setPadding(0,10,0,10);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
TextView tv_item1 = new TextView(this);
TextView tv_item2 = new TextView(this);
TextView tv_item3 = new TextView(this);

因为您多次尝试放置相同的文本视图。您只能使用它一次,因此必须一次又一次地实例化它:

// you remove the definition of the text views here and you put it inside the loop
    for (ArrayList<String> al : l){
        int i = 0;
        for(String s : al){
 TextView tv_item2 = new TextView(this);
TextView tv_item1 = new TextView(this);
TextView tv_item3 = new TextView(this);
            if (i == 0){
                i++;

                tv_item1.setText(s);
                tv_item1.setGravity(Gravity.CENTER);
            }
            if (i == 1){

                tv_item2.setText(s);
                tv_item2.setGravity(Gravity.CENTER);
                i++;
            }
            if (i == 2){

                tv_item3.setText(s);
                tv_item3.setGravity(Gravity.CENTER);
                tr.addView(tv_item1);
                tr.addView(tv_item2);
                tr.addView(tv_item3);
                table.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            }
        }
    }

这不是最好的方法,你应该创建一个以i为参数的方法,并返回一个带有重心的文本视图。。。还有你的员工

因为您多次尝试放置相同的文本视图。您只能使用它一次,因此必须一次又一次地实例化它:

// you remove the definition of the text views here and you put it inside the loop
    for (ArrayList<String> al : l){
        int i = 0;
        for(String s : al){
 TextView tv_item2 = new TextView(this);
TextView tv_item1 = new TextView(this);
TextView tv_item3 = new TextView(this);
            if (i == 0){
                i++;

                tv_item1.setText(s);
                tv_item1.setGravity(Gravity.CENTER);
            }
            if (i == 1){

                tv_item2.setText(s);
                tv_item2.setGravity(Gravity.CENTER);
                i++;
            }
            if (i == 2){

                tv_item3.setText(s);
                tv_item3.setGravity(Gravity.CENTER);
                tr.addView(tv_item1);
                tr.addView(tv_item2);
                tr.addView(tv_item3);
                table.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            }
        }
    }

这不是最好的方法,你应该创建一个以i为参数的方法,并返回一个带有重心的文本视图。。。还有你的员工

可能重复的thx,最终我理解了这位家长的问题,p.s.i++;必须把周期opsthx结束,最后我理解了这个家长的问题,p.s.i++;必须结束周期操作