创建 table Android Studio 时遇到问题

Have a problem on creating table Android Studio

我正在尝试制作一个简单的应用程序。该程序应创建一个具有给定行号和列号的 table。我在下面添加我的 Java 和 xml 文件:

public class MainActivity extends AppCompatActivity {

    private Button btnCreate, btnCalculate, btnReset, btnExit;
    private EditText txtColumn, txtRow;
    private LinearLayout linLayout, mainLayout;
    private TextView txtResult;
    private Random random;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnCreate = (Button) findViewById(R.id.btnCreate);
        btnCalculate = (Button) findViewById(R.id.btnCalculate);
        btnReset = (Button) findViewById(R.id.btnReset);
        btnExit = (Button) findViewById(R.id.btnExit);

        txtColumn = (EditText) findViewById(R.id.txtColumn);
        txtRow = (EditText) findViewById(R.id.txtRow);
        txtResult = (TextView) findViewById(R.id.txtResult);
        txtColumn.requestFocus();

        random = new Random();

        LinearLayout mainLayout = findViewById(R.id.mainLayout);
        TableLayout table = new TableLayout(this);

        btnCalculate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });

        btnCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                table.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                table.setShrinkAllColumns(true);
                table.setStretchAllColumns(true);
                int columnNumber = Integer.parseInt(txtColumn.getText().toString());
                int rowNumber = Integer.parseInt(txtRow.getText().toString());

                for (int i=0; i < rowNumber; i++) {
                    TableRow row = new TableRow(MainActivity.this);
                    for (int j=0; j < columnNumber; j++) {
                        int value = random.nextInt(100) + 1;
                        TextView tv = new TextView(MainActivity.this);
                        tv.setText(String.valueOf(value));
                        row.addView(tv);
                    }
                    table.addView(row);
                }

                mainLayout.addView(table);

            }
        });


        btnExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.exit(0);
            }
        });
        btnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                txtColumn.setText(null);
                txtRow.setText(null);
                txtResult.setText(null);
            }
        });
    }
}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <EditText
        android:id="@+id/txtRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter row number"
        android:inputType="textCapWords"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/txtColumn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter column number"
        android:inputType="textCapWords"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/txtResult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Result"
        android:background="#E91E63"
        android:textSize="18sp"
        />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnCreate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CREATE"/>

        <Button
            android:id="@+id/btnCalculate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CALCULATE" />

        <Button
            android:id="@+id/btnReset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RESET" />
        <Button
            android:id="@+id/btnExit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="EXIT" />
    </LinearLayout>

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


    </LinearLayout>

</LinearLayout>

这是我收到的错误消息:

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

因此,每当我尝试将 mainLayout.removeView(table); 添加到代码中时,单击创建按钮时都没有任何反应。谢谢你们。我将不胜感激任何类型的评论。

您有以下错误:

1:mainLayout布局上方的布局让高度为match_parent。使 mainLayout 不可显示。

2:布局 mainLayout 也将高度设置为 match_parent,这也是错误的。不知道你明白match_parent是什么意思吗?

3: 第一次添加成功,但是因为设置高度为match_parent,导致不可见,其实是成功添加了table。第二次点击因为已经添加到table,会崩溃

下面的代码我已经为你修复了,让我们试试吧。

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <EditText
        android:id="@+id/txtRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter row number"
        android:inputType="textCapWords"
        android:textSize="18sp"
        android:text="3"/>

    <EditText
        android:id="@+id/txtColumn"
        android:text="2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter column number"
        android:inputType="textCapWords"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/txtResult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Result"
        android:background="#E91E63"
        android:textSize="18sp"
        />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnCreate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CREATE"/>

        <Button
            android:id="@+id/btnCalculate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CALCULATE" />

        <Button
            android:id="@+id/btnReset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RESET" />
        <Button
            android:id="@+id/btnExit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="EXIT" />
    </LinearLayout>

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


    </LinearLayout>

</LinearLayout>

JAVA:

public class MainAct extends AppCompatActivity {

    private Button btnCreate, btnCalculate, btnReset, btnExit;
    private EditText txtColumn, txtRow;
    private LinearLayout linLayout, mainLayout;
    private TextView txtResult;
    private Random random;
    TableLayout table;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnCreate = (Button) findViewById(R.id.btnCreate);
        btnCalculate = (Button) findViewById(R.id.btnCalculate);
        btnReset = (Button) findViewById(R.id.btnReset);
        btnExit = (Button) findViewById(R.id.btnExit);

        txtColumn = (EditText) findViewById(R.id.txtColumn);
        txtRow = (EditText) findViewById(R.id.txtRow);
        txtResult = (TextView) findViewById(R.id.txtResult);
        txtColumn.requestFocus();

        random = new Random();

        LinearLayout mainLayout = findViewById(R.id.mainLayout);


        btnCalculate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });

        btnCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (table != null) {
                    mainLayout.removeView(table);
                }
                table = new TableLayout(getBaseContext());

                table.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                table.setShrinkAllColumns(true);
                table.setStretchAllColumns(true);
                int columnNumber = Integer.parseInt(txtColumn.getText().toString());
                int rowNumber = Integer.parseInt(txtRow.getText().toString());

                for (int i=0; i < rowNumber; i++) {
                    TableRow row = new TableRow(MainAct.this);
                    for (int j=0; j < columnNumber; j++) {
                        int value = random.nextInt(100) + 1;
                        TextView tv = new TextView(MainAct.this);
                        tv.setText(String.valueOf(value));
                        row.addView(tv);
                    }
                    table.addView(row);
                }
                if (table != null) {
                    mainLayout.addView(table);
                }
            }
        });


        btnExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.exit(0);
            }
        });
        btnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                txtColumn.setText(null);
                txtRow.setText(null);
                txtResult.setText(null);
            }
        });
    }
}