【问题标题】:Initializing TableLayout in XML and programmatically filling it - Android (java)在 XML 中初始化 TableLayout 并以编程方式填充它 - Android (java)
【发布时间】:2012-12-14 00:30:31
【问题描述】:

我正在尝试用 XML 初始化 TableLayout,然后以编程方式填充它。这是我的 XML、java 和 LogCat 输出。

Highscores.java

public class Highscores extends Activity {

    TableLayout table;
    TableRow rowHeader, row1, row2, row3, row4, row5, row6, row7, row8, row9, row10;
    TextView rank, percentage, score;
    Button btn1;    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.highscoresmain);

        TableLayout table = (TableLayout)findViewById(R.id.tableLayout);

        TextView rank = (TextView)findViewById(R.id.rank);
        rank.setText("RANK");
        TextView percentage = (TextView)findViewById(R.id.percentage);
        percentage.setText("PERCENTAGE");
        TextView score = (TextView)findViewById(R.id.score);
        score.setText("SCORE");

        TableRow rowHeader = (TableRow)findViewById(R.id.rowHeader);

        rowHeader.addView(rank);  //Line 39
        rowHeader.addView(percentage);
        rowHeader.addView(score);

        Button btn1 = (Button)findViewById(R.id.homeBtn);

        table.addView(rowHeader, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        btn1.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Intent intent = new Intent(Highscores.this, MainMenu.class);
                startActivity(intent);
            }
        });
    }
}

highscoresmain.xml

<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id = "@+id/tableLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0"
    android:padding="5dp">

    <TableRow 
        android:layout_height="wrap_content"
        android:id="@+id/rowHeader">
        <TextView
            android:id="@+id/rank"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/percentage"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/score"
            android:layout_height="wrap_content" />
    </TableRow>
      </TableLayout>

LogCat

12-19 06:03:36.960: E/AndroidRuntime(20130): FATAL EXCEPTION: main
12-19 06:03:36.960: E/AndroidRuntime(20130): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.Highscores}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
12-19 06:03:36.960: E/AndroidRuntime(20130):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-19 06:03:36.960: E/AndroidRuntime(20130):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-19 06:03:36.960: E/AndroidRuntime(20130):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-19 06:03:36.960: E/AndroidRuntime(20130):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-19 06:03:36.960: E/AndroidRuntime(20130):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 06:03:36.960: E/AndroidRuntime(20130):    at android.os.Looper.loop(Looper.java:137)
12-19 06:03:36.960: E/AndroidRuntime(20130):    at android.app.ActivityThread.main(ActivityThread.java:4745)
12-19 06:03:36.960: E/AndroidRuntime(20130):    at java.lang.reflect.Method.invokeNative(Native Method)
12-19 06:03:36.960: E/AndroidRuntime(20130):    at java.lang.reflect.Method.invoke(Method.java:511)
12-19 06:03:36.960: E/AndroidRuntime(20130):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-19 06:03:36.960: E/AndroidRuntime(20130):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-19 06:03:36.960: E/AndroidRuntime(20130):    at dalvik.system.NativeStart.main(Native Method)
12-19 06:03:36.960: E/AndroidRuntime(20130): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
12-19 06:03:36.960: E/AndroidRuntime(20130):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3378)
12-19 06:03:36.960: E/AndroidRuntime(20130):    at android.view.ViewGroup.addView(ViewGroup.java:3249)
12-19 06:03:36.960: E/AndroidRuntime(20130):    at android.view.ViewGroup.addView(ViewGroup.java:3194)
12-19 06:03:36.960: E/AndroidRuntime(20130):    at android.view.ViewGroup.addView(ViewGroup.java:3170)
12-19 06:03:36.960: E/AndroidRuntime(20130):    at com.example.test.Highscores.onCreate(Highscores.java:39)
12-19 06:03:36.960: E/AndroidRuntime(20130):    at android.app.Activity.performCreate(Activity.java:5008)
12-19 06:03:36.960: E/AndroidRuntime(20130):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-19 06:03:36.960: E/AndroidRuntime(20130):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-19 06:03:36.960: E/AndroidRuntime(20130):    ... 11 more

我想要的是一个简单的 3 列表,其中第 1 行的标题是“RANK”、“PERCENTAGE”和“SCORE”。稍后我将从 SQLite 数据库中提取另外 10 行,但我会在之后解决这个问题。

【问题讨论】:

    标签: java android xml tablelayout


    【解决方案1】:

    您的 xml 文件将被编辑;

    我正在尝试用 XML 初始化 TableLayout,然后以编程方式填充它。这是我的 XML、java 和 LogCat 输出。

    highscoresmain.xml

    <TableLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id = "@+id/tableLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:padding="5dp">
    
    
       </TableLayout>
    

    高分课程内容;

    public class Highscores extends Activity {
    
        TableLayout table;
        TableRow rowHeader, row1, row2, row3, row4, row5, row6, row7, row8, row9, row10;
        TextView rank, percentage, score;
        Button btn1;    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.highscoresmain);
    
            TableLayout table = (TableLayout)findViewById(R.id.tableLayout);
    
            TextView rank = new TextView(this);
            rank.setText("RANK");
            TextView percentage = new TextView(this);
            percentage.setText("PERCENTAGE");
            TextView score = new TextView(this);
            score.setText("SCORE");
    
            TableRow rowHeader = new TableRow(this);
    
            rowHeader.addView(rank);  //Line 39
            rowHeader.addView(percentage);
            rowHeader.addView(score);
    
            Button btn1 = (Button)findViewById(R.id.homeBtn);
    
            table.addView(rowHeader);
    
            btn1.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    Intent intent = new Intent(Highscores.this, MainMenu.class);
                    startActivity(intent);
                }
            });
        }
    }
    

    【讨论】:

    • 当我调用 TableLayout 的 addview 时,它会产生异常。为什么?
    • 你添加哪个视图以及如何添加?
    • 我想添加包含两个文本视图的表格行
    • 你得到了哪个例外?
    【解决方案2】:

    您不需要以下代码:

    TableRow rowHeader = (TableRow)findViewById(R.id.rowHeader);
    
    rowHeader.addView(rank);  //Line 39
    rowHeader.addView(percentage);
    rowHeader.addView(score);
    

    table.addView(rowHeader, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    因为它们已经在布局中定义了。

    【讨论】:

      【解决方案3】:

      答案在你的logcat

      java.lang.RuntimeException:无法启动活动 组件信息{com.example.test/com.example.test.Highscores}: java.lang.IllegalStateException:指定的孩子已经有一个 父母。您必须先在孩子的父母上调用 removeView()。

      TextViews 已经在TableRow 中,如果你想以编程方式填充它,只需删除TableLayout 的所有子代,在代码中创建它们并将它们添加到Activity 的@ 987654326@ 就像你刚才所做的那样。

      祝你好运!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-08
        • 1970-01-01
        • 2017-04-07
        • 2013-09-13
        相关资源
        最近更新 更多