创建表Android Studio时遇到问题
Posted
技术标签:
【中文标题】创建表Android Studio时遇到问题【英文标题】:Have a problem on creating table Android Studio 【发布时间】:2022-01-15 20:29:48 【问题描述】:我正在尝试制作一个简单的应用程序。该程序应创建一个具有给定行号和列号的表。我在下面添加我的 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_
android:layout_
android:orientation="vertical" >
<EditText
android:id="@+id/txtRow"
android:layout_
android:layout_
android:hint="Enter row number"
android:inputType="textCapWords"
android:textSize="18sp" />
<EditText
android:id="@+id/txtColumn"
android:layout_
android:layout_
android:hint="Enter column number"
android:inputType="textCapWords"
android:textSize="18sp" />
<TextView
android:id="@+id/txtResult"
android:layout_
android:layout_
android:hint="Result"
android:background="#E91E63"
android:textSize="18sp"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="horizontal" >
<Button
android:id="@+id/btnCreate"
android:layout_
android:layout_
android:text="CREATE"/>
<Button
android:id="@+id/btnCalculate"
android:layout_
android:layout_
android:text="CALCULATE" />
<Button
android:id="@+id/btnReset"
android:layout_
android:layout_
android:text="RESET" />
<Button
android:id="@+id/btnExit"
android:layout_
android:layout_
android:text="EXIT" />
</LinearLayout>
<LinearLayout
android:id="@+id/mainLayout"
android:layout_
android:layout_
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);
添加到代码中时,当我单击创建按钮时,什么都没有发生。谢谢你们。我将不胜感激任何类型的 cmets。
【问题讨论】:
【参考方案1】:您有以下错误:
1:mainLayout 布局上方的布局让高度为match_parent。使 mainLayout 无法显示。
2:布局 mainLayout 也将高度设置为 match_parent,这也是错误的。我想知道你明白 match_parent 是什么意思吗?
3:第一次添加成功,但是因为设置高度为match_parent,导致看不到,实际上是成功添加了table。第二次点击因为已经添加到表格,会崩溃。
下面的代码我已经给你修好了,我们试试吧。
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linLayout"
android:layout_
android:layout_
android:orientation="vertical" >
<EditText
android:id="@+id/txtRow"
android:layout_
android:layout_
android:hint="Enter row number"
android:inputType="textCapWords"
android:textSize="18sp"
android:text="3"/>
<EditText
android:id="@+id/txtColumn"
android:text="2"
android:layout_
android:layout_
android:hint="Enter column number"
android:inputType="textCapWords"
android:textSize="18sp" />
<TextView
android:id="@+id/txtResult"
android:layout_
android:layout_
android:hint="Result"
android:background="#E91E63"
android:textSize="18sp"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="horizontal" >
<Button
android:id="@+id/btnCreate"
android:layout_
android:layout_
android:text="CREATE"/>
<Button
android:id="@+id/btnCalculate"
android:layout_
android:layout_
android:text="CALCULATE" />
<Button
android:id="@+id/btnReset"
android:layout_
android:layout_
android:text="RESET" />
<Button
android:id="@+id/btnExit"
android:layout_
android:layout_
android:text="EXIT" />
</LinearLayout>
<LinearLayout
android:id="@+id/mainLayout"
android:layout_
android:layout_
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);
);
【讨论】:
以上是关于创建表Android Studio时遇到问题的主要内容,如果未能解决你的问题,请参考以下文章
转换 Android Studio 项目新 API 密钥时遇到问题