Android 在xml中使用两个布局,但出现错误:必须对子项&x27;调用removeView();她先是父母

Android 在xml中使用两个布局,但出现错误:必须对子项&x27;调用removeView();她先是父母,android,Android,我已经搜索了该问题的答案,但无法根据我的情况找到解决方案。我正在xml布局中使用两个表布局,并将动态创建的行添加到这些布局中。每行包含更多视图。 这是我的密码 Button b[]; EditText ed[]; TableLayout layoutTable,layoutButton; LinearLayout lLayoutTable,lLayoutButton; int value,answer; TableRow row[],rowb[]; TextView tv[],tv1[],tv2

我已经搜索了该问题的答案,但无法根据我的情况找到解决方案。我正在xml布局中使用两个表布局,并将动态创建的行添加到这些布局中。每行包含更多视图。 这是我的密码

Button b[];
EditText ed[];
TableLayout layoutTable,layoutButton;
LinearLayout lLayoutTable,lLayoutButton;
int value,answer;
TableRow row[],rowb[];
TextView tv[],tv1[],tv2[],tv3[];

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.table_game_layout);
    Intent intent = getIntent();
    value = intent.getIntExtra("keyInt", 0);

    layoutTable    = (TableLayout)findViewById(R.id.layoutTable);
    layoutButton   = (TableLayout)findViewById(R.id.layoutButton);
    layoutTable.removeAllViews();
    layoutButton.removeAllViews();

    tv   = new TextView[17];
    row  = new TableRow[17];
    tv1  = new TextView[17];
    tv2  = new TextView[17];
    tv3  = new TextView[17];
    ed   = new EditText[17];
    b    = new Button[17];
    rowb = new TableRow[5];

         LayoutParams params = new LayoutParams(android.support.v4.view.ViewPager.LayoutParams.WRAP_CONTENT,
                 android.support.v4.view.ViewPager.LayoutParams.MATCH_PARENT);

            layoutTable.setLayoutParams(params);
            layoutButton.setLayoutParams(params);

            android.widget.TableRow.LayoutParams params1 = new TableRow.LayoutParams(0,LayoutParams.WRAP_CONTENT,1f);

            for(int i=1; i<=16; i++){

                tv[i]  = new TextView(this);
                tv1[i] = new TextView(this);
                tv2[i] = new TextView(this);
                tv3[i] = new TextView(this);
                ed[i]  = new EditText(this);
                row[i] = new TableRow(this);
                row[i].removeAllViews();
                tv[i].setLayoutParams(params1);
                tv1[i].setLayoutParams(params1);
                tv2[i].setLayoutParams(params1);
                ed[i].setLayoutParams(params1);
                tv3[i].setLayoutParams(params1);
                tv[i].setTextSize(30);
                tv1[i].setTextSize(30);
                tv2[i].setTextSize(30);
                tv3[i].setTextSize(30);
                ed[i].setTextSize(30);
                tv[i].setText(""+value);
                tv1[i].setText("x");
                tv2[i].setText(""+i);
                tv3[i].setText("=");
                row[i].addView(tv[i]);
                row[i].addView(tv1[i]);
                row[i].addView(tv2[i]);
                row[i].addView(tv3[i]);
                row[i].addView(ed[i]);

                answer= i*value;
                b[i] = new Button(this);
                b[i].setText(""+answer);

                layoutTable.addView(row[i]);

            }

            for(int i=1; i<=4; i++){
                int j=1;
                rowb[i] = new TableRow(this);
                rowb[i].removeAllViews();
                rowb[i].addView(b[j]);
                rowb[i].addView(b[j+1]);
                rowb[i].addView(b[j+2]);
                rowb[i].addView(b[j+3]);
                j+=4;
                layoutButton.addView(rowb[i]);
      }

显示logcat错误我还更新了代码。第105行是什么?旁注:在java中,数组是以0为基础的,第105行是指“j+=4”?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:orientation="vertical" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/lLayoutTable"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TableLayout
                android:id="@+id/layoutTable"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >


            </TableLayout>

        </LinearLayout>
    </ScrollView>

</LinearLayout>

<LinearLayout
    android:id="@+id/lLayoutButton"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="vertical" >

    <TableLayout
        android:id="@+id/layoutButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >


    </TableLayout>

</LinearLayout>
07-14 14:57:53.382: E/AndroidRuntime(5538): FATAL EXCEPTION: main
07-14 14:57:53.382: E/AndroidRuntime(5538): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tablegame/com.example.tablegame.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
07-14 14:57:53.382: E/AndroidRuntime(5538):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-14 14:57:53.382: E/AndroidRuntime(5538):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-14 14:57:53.382: E/AndroidRuntime(5538):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-14 14:57:53.382: E/AndroidRuntime(5538):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-14 14:57:53.382: E/AndroidRuntime(5538):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-14 14:57:53.382: E/AndroidRuntime(5538):     at android.os.Looper.loop(Looper.java:137)
07-14 14:57:53.382: E/AndroidRuntime(5538):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-14 14:57:53.382: E/AndroidRuntime(5538):     at java.lang.reflect.Method.invokeNative(Native Method)
07-14 14:57:53.382: E/AndroidRuntime(5538):     at java.lang.reflect.Method.invoke(Method.java:511)
07-14 14:57:53.382: E/AndroidRuntime(5538):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-14 14:57:53.382: E/AndroidRuntime(5538):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-14 14:57:53.382: E/AndroidRuntime(5538):     at dalvik.system.NativeStart.main(Native Method)
07-14 14:57:53.382: E/AndroidRuntime(5538): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
07-14 14:57:53.382: E/AndroidRuntime(5538):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3339)
07-14 14:57:53.382: E/AndroidRuntime(5538):     at android.view.ViewGroup.addView(ViewGroup.java:3210)
07-14 14:57:53.382: E/AndroidRuntime(5538):     at android.view.ViewGroup.addView(ViewGroup.java:3155)
07-14 14:57:53.382: E/AndroidRuntime(5538):     at android.view.ViewGroup.addView(ViewGroup.java:3131)
07-14 14:57:53.382: E/AndroidRuntime(5538):     at com.example.tablegame.MainActivity.onCreate(MainActivity.java:105)
07-14 14:57:53.382: E/AndroidRuntime(5538):     at android.app.Activity.performCreate(Activity.java:5104)
07-14 14:57:53.382: E/AndroidRuntime(5538):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-14 14:57:53.382: E/AndroidRuntime(5538):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-14 14:57:53.382: E/AndroidRuntime(5538):     ... 11 more