java - 将 TextView 设置为 TableRow,IllegalStateException - Android

标签 java android textview tablerow illegalstateexception

我正在尝试使用 SQLite 为我的问答游戏实现高分表。我在我进入代码的错误中评论了。

我没有在其他地方使用错误中提到的那些 View ,所以我不知道为什么会收到该错误。

Highscores.java

public class Highscores extends Activity {

    DatabaseHelper dh;
    SQLiteDatabase db;
    Cursor percentages, scores;
    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);

        dh = new DatabaseHelper(this);
        db = dh.openDB();
        percentages = dh.getPercentage(db);
        scores = dh.getScore(db);

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

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

        TextView rank = (TextView)findViewById(R.id.rank);
        TextView percentage = (TextView)findViewById(R.id.percentage);
        TextView score = (TextView)findViewById(R.id.score);

        TextView r1r = (TextView)findViewById(R.id.r1r);
        TextView r1p = (TextView)findViewById(R.id.r1p);
        TextView r1s = (TextView)findViewById(R.id.r1s);

        rank.setText("TEST - COLUMN RANK");
        percentage.setText("TEST - COLUMN PERCENTAGE");
        score.setText("TEST - COLUMN SCORE");
        r1r.setText("test..rank");
        r1p.setText("teset...percentage");
        r1s.setText("test...scoree");

        rowHeader.addView(rank);  //IllegalStateException:  The specified child already has a parent. You must call removeView() on the child's parent first.
        rowHeader.addView(percentage);
        rowHeader.addView(score);

        row1.addView(r1r);
        row1.addView(r1p);
        row1.addView(r1s);

        table.addView(rowHeader);
        table.addView(row1); 

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

        dh.closeDB(db);
    }
}

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:id="@+id/rowHeader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/rank"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/percentage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/score"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    <TableRow
        android:id="@+id/row1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/r1r"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/r1p"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/r1s"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

</TableLayout>

日志猫

01-02 15:21:10.870: W/dalvikvm(901): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
01-02 15:21:10.938: E/AndroidRuntime(901): FATAL EXCEPTION: main
01-02 15:21:10.938: E/AndroidRuntime(901): 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.
01-02 15:21:10.938: E/AndroidRuntime(901):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-02 15:21:10.938: E/AndroidRuntime(901):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-02 15:21:10.938: E/AndroidRuntime(901):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-02 15:21:10.938: E/AndroidRuntime(901):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-02 15:21:10.938: E/AndroidRuntime(901):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-02 15:21:10.938: E/AndroidRuntime(901):  at android.os.Looper.loop(Looper.java:137)
01-02 15:21:10.938: E/AndroidRuntime(901):  at android.app.ActivityThread.main(ActivityThread.java:4745)
01-02 15:21:10.938: E/AndroidRuntime(901):  at java.lang.reflect.Method.invokeNative(Native Method)
01-02 15:21:10.938: E/AndroidRuntime(901):  at java.lang.reflect.Method.invoke(Method.java:511)
01-02 15:21:10.938: E/AndroidRuntime(901):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-02 15:21:10.938: E/AndroidRuntime(901):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-02 15:21:10.938: E/AndroidRuntime(901):  at dalvik.system.NativeStart.main(Native Method)
01-02 15:21:10.938: E/AndroidRuntime(901): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
01-02 15:21:10.938: E/AndroidRuntime(901):  at android.view.ViewGroup.addViewInner(ViewGroup.java:3378)
01-02 15:21:10.938: E/AndroidRuntime(901):  at android.view.ViewGroup.addView(ViewGroup.java:3249)
01-02 15:21:10.938: E/AndroidRuntime(901):  at android.view.ViewGroup.addView(ViewGroup.java:3194)
01-02 15:21:10.938: E/AndroidRuntime(901):  at android.view.ViewGroup.addView(ViewGroup.java:3170)
01-02 15:21:10.938: E/AndroidRuntime(901):  at com.example.test.Highscores.onCreate(Highscores.java:73)
01-02 15:21:10.938: E/AndroidRuntime(901):  at android.app.Activity.performCreate(Activity.java:5008)
01-02 15:21:10.938: E/AndroidRuntime(901):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-02 15:21:10.938: E/AndroidRuntime(901):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
01-02 15:21:10.938: E/AndroidRuntime(901):  ... 11 more

提前谢谢您!

最佳答案

异常(exception)情况非常明显:

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

这就是rank,它已经有一个父级,rowHeader它是父级。正如您在布局中指定的那样,这些 View 已经位于 TableRow 元素内。您不需要重新添加它们,事实上您也做不到。

关于java - 将 TextView 设置为 TableRow,IllegalStateException - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14129752/

相关文章:

java - 在 catch 上调用什么 ASM 访问者方法来进行类型注释

java - Java 中的 Rest Web 服务 - 从数据库中删除行

java - 解析 JSON 数组

android - 在 Android 设备上评估应用程序性能的好方法

android - 如何从android中的特定点以特定方向(滑动方向)显示文本

android - 如何在 for 循环中实现一个 TextView 对象数组?

java - Spring 如何使用 Java 8 类,却又在 Java 7 上运行?

android - 此构建中使用了已弃用的 Gradle 功能,使其与 Gradle 6 不兼容

java.lang.NumberFormatException : Invalid int: "3546504756", 这个错误是什么意思?

Android:显示一个整数