如何在android中运行时在单个表行中显示多个文本视图?

时间:2012-10-27 07:56:20

标签: android sqlite textview android-tablelayout

我的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

我在代码中犯了错误?有人能告诉我吗?提前谢谢。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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);