在TableRow中添加多个addView

时间:2016-12-24 20:39:12

标签: java android xml android-layout textview

我一直在寻找这个问题几个小时。我试图在Android Studio中连续创建一个包含多个值的表。但是,我不想硬编码“单元格”,因此我尝试使用Java动态创建表行和单元格。不幸的是,当我尝试添加单元格时,它给了我这个错误:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3770)
at android.view.ViewGroup.addView(ViewGroup.java:3623)
at android.widget.TableLayout.addView(TableLayout.java:429)
at android.view.ViewGroup.addView(ViewGroup.java:3568)
at android.widget.TableLayout.addView(TableLayout.java:411)
at android.view.ViewGroup.addView(ViewGroup.java:3544)
at android.widget.TableLayout.addView(TableLayout.java:402)
at com.abukatech.tabletest2.MainActivity.test(MainActivity.java:32)
at com.abukatech.tabletest2.MainActivity$1.onClick(MainActivity.java:20)
at android.view.View.performClick(View.java:4654)
at android.view.View$PerformClick.run(View.java:19438)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5487)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)

这是我的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.abukatech.tabletest2.MainActivity">

<Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:layout_marginStart="116dp"
    android:layout_marginTop="59dp"
    android:id="@+id/button" />

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:id="@+id/tabletest">
</TableLayout>

这是我的Java:

package com.abukatech.tabletest2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            test();
        }
    });
}

String[] test = {"buff", "meh", "plz!"};
public void test() {
    TableLayout tbl = (TableLayout) findViewById(R.id.tabletest);
    for (int i = 0; i < 2; i++) {
        TextView testview = new TextView(this);
        testview.setText(test[i]);
        // I think this is where my problem is
        tbl.addView(testview);
        tbl.addView(testview);
    }
}

}

我也听说过“膨胀”TextView,但找不到任何关于它的信息。如果您了解它,或者有更好的方法将TextView动态添加到TableRow,请告诉我,因为我是一个完整的Java菜鸟。 :d

1 个答案:

答案 0 :(得分:1)

除了将TextView添加到TableLayout的部分外,大多数代码都是正确的。问题是你要两次添加相同的TextView对象。这会导致您看到的异常。

删除第二个tbl.addView(testview)电话,您的代码应该没问题。