我正在尝试使用SQLite为我的琐事游戏实现一个高分表。我在错误中评论了我的代码。
我没有在其他任何地方使用错误中提到的那些视图,所以我不知道为什么我会收到该错误。
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>
logcat的
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
提前谢谢!
答案 0 :(得分:1)
异常非常清楚:
java.lang.IllegalStateException
:指定的孩子已经有父母。您必须首先在孩子的父母身上调用removeView()。
那是rank
,它已经有父,rowHeader
它是父。这些视图已经在您在布局中指定的TableRow
元素中。你不需要重新添加它们,事实上你不能重新添加它们。