0

我的 sqlite 数据库中有一些表。我获取表值并存储到数组中。现在我想在运行时动态显示多个文本视图。我的代码是http://pastie.org/5122521。我的 logcat 显示了一些像这样的错误

  10-27 11:24:22.444: INFO/System.out(552): seek bar value---->-77
  10-27 13:04:15.335: ERROR/AndroidRuntime(868): FATAL EXCEPTION: main
  10-27 13:04:15.335: ERROR/AndroidRuntime(868): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.exale.newfinace/com.exale.newfinace.Income}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
  10-27 13:04:15.335: ERROR/AndroidRuntime(868):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
  10-27 13:04:15.335: ERROR/AndroidRuntime(868):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
  10-27 13:04:15.335: ERROR/AndroidRuntime(868):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
  10-27 13:04:15.335: ERROR/AndroidRuntime(868):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
 10-27 13:04:15.335: ERROR/AndroidRuntime(868):     at android.os.Handler.dispatchMessage(Handler.java:99)
 10-27 13:04:15.335: ERROR/AndroidRuntime(868):     at android.os.Looper.loop(Looper.java:123)
 10-27 13:04:15.335: ERROR/AndroidRuntime(868):     at android.app.ActivityThread.main(ActivityThread.java:3683)
 10-27 13:04:15.335: ERROR/AndroidRuntime(868):     at java.lang.reflect.Method.invokeNative(Native Method)
 10-27 13:04:15.335: ERROR/AndroidRuntime(868):     at java.lang.reflect.Method.invoke(Method.java:507)
 10-27 13:04:15.335: ERROR/AndroidRuntime(868):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
 10-27 13:04:15.335: ERROR/AndroidRuntime(868):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 10-27 13:04:15.335: ERROR/AndroidRuntime(868):     at dalvik.system.NativeStart.main(Native Method)
 10-27 13:04:15.335: ERROR/AndroidRuntime(868): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
 10-27 13:04:15.335: ERROR/AndroidRuntime(868):     at android.view.ViewGroup.addViewInner(ViewGroup.java:1976)
 10-27 13:04:15.335: ERROR/AndroidRuntime(868):     at android.view.ViewGroup.addView(ViewGroup.java:1871)
 10-27 13:04:15.335: ERROR/AndroidRuntime(868):     at android.widget.TableLayout.addView(TableLayout.java:421)
 10-27 13:04:15.335: ERROR/AndroidRuntime(868):     at android.view.ViewGroup.addView(ViewGroup.java:1851)
 10-27 13:04:15.335: ERROR/AndroidRuntime(868):     at android.widget.TableLayout.addView(TableLayout.java:412)
 10-27 13:04:15.335: ERROR/AndroidRuntime(868):     at com.exale.newfinace.Income.onCreate(Income.java:402)
 10-27 13:04:15.335: ERROR/AndroidRuntime(868):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 10-27 13:04:15.335: ERROR/AndroidRuntime(868):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
 10-27 13:04:15.335: ERROR/AndroidRuntime(868):     ... 11 more

我在哪里做错了代码?谁能告诉我?提前致谢。

4

2 回答 2

0

试试下面的代码:

do{
TableRow tr_row1 = new TableRow(this);
TextView tv=new TextView(this); // in place of this you can use your activity reference
tr_row1.add(tv);
}while(condition);
于 2012-10-27T08:52:43.553 回答
0

可能您已经创建了一个 TextView 对象,并且您正在多次添加它。可能处于产生问题的循环中。由于您不能将单个视图对象添加到视图层次结构树,因为这会破坏树属性并创建一个循环

于 2012-10-27T08:01:28.410 回答