我的代码在这里
if(c!=null)
{
c.moveToFirst();
String col = c.getString(2); //
check.setText(col);
check.setVisibility(0);
while(!c.isAfterLast())
{
String col1 = c.getString(1);
String col2 = c.getString(2);
String col3 = c.getString(3);
while(!c.isAfterLast())
{
TextView que1 = new TextView(this);
que1.setText(col1);
lymn.addView(que1);
if(col3.equals("Date"))
{
DatePicker dp = new DatePicker(this);
lymn.addView(dp);
break;
}
if(col3.equals("User Input"))
{
EditText ed = new EditText(this);
ed.setWidth(250);
lymn.addView(ed);
break;
}
if(col3.equals("YES/NO"))
{
yes1.setText("Yes");
no1.setText("NO");
rg1.addView(yes1);
rg1.addView(no1);
lymn.addView(rg1);
break;
}
if(col3.equals("High Average Low"))
{
High1.setText("High");
Avg1.setText("Average");
Low1.setText("Low");
rg2.addView(High1);
rg2.addView(Avg1);
rg2.addView(Low1);
lymn.addView(rg2);
break;
}
if(col3.equals("Excellent Good Average Poor"))
{
exce1.setText("Excellent");
good1.setText("Good");
avg11.setText("Average");
poor1.setText("Poor");
rg3.addView(exce1);
rg3.addView(good1);
rg3.addView(avg11);
rg3.addView(poor1);
lymn.addView(rg3);
break;
}
break;
}
c.moveToNext();
}
}
当我像这样运行此代码igot错误时
09-06 11:34:37.777: WARN/Exception(27814): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
09-06 11:34:37.777: WARN/Exception(27814): at android.view.ViewGroup.addViewInner(ViewGroup.java:1970)
09-06 11:34:37.777: WARN/Exception(27814): at android.view.ViewGroup.addView(ViewGroup.java:1865)
09-06 11:34:37.777: WARN/Exception(27814): at android.view.ViewGroup.addView(ViewGroup.java:1822)
09-06 11:34:37.777: WARN/Exception(27814): at android.view.ViewGroup.addView(ViewGroup.java:1802)
09-06 11:34:37.777: WARN/Exception(27814): at com.ezee.app.user1.LoadCheckIn(user1.java:256)
09-06 11:34:37.777: WARN/Exception(27814): at com.ezee.app.user1.onCreate(user1.java:61)
09-06 11:34:37.777: WARN/Exception(27814): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-06 11:34:37.777: WARN/Exception(27814): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-06 11:34:37.777: WARN/Exception(27814): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-06 11:34:37.777: WARN/Exception(27814): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-06 11:34:37.777: WARN/Exception(27814): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-06 11:34:37.777: WARN/Exception(27814): at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 11:34:37.777: WARN/Exception(27814): at android.os.Looper.loop(Looper.java:123)
09-06 11:34:37.777: WARN/Exception(27814): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-06 11:34:37.777: WARN/Exception(27814): at java.lang.reflect.Method.invokeNative(Native Method)
09-06 11:34:37.777: WARN/Exception(27814): at java.lang.reflect.Method.invoke(Method.java:521)
09-06 11:34:37.777: WARN/Exception(27814): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-06 11:34:37.777: WARN/Exception(27814): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-06 11:34:37.777: WARN/Exception(27814): at dalvik.system.NativeStart.main(Native Method)
PLZ帮助我
答案 0 :(得分:40)
您收到的错误很难找到,因为您的代码遍布整个地方,但错误意味着您正在添加View
(子)已经在屏幕上(已有父级)。< / p>
这可能是任何地方,很难说这篇文章,但例如:
这个可能有效:
EditText ed = new EditText(this);
ed.setWidth(250);
lymn.addView(ed);
ed
是新的,所以没有父母。
但是我找不到yes1
的声明,所以这一个可能是罪魁祸首。或者也许no1
。
yes1.setText("Yes");
no1.setText("NO");
rg1.addView(yes1);
rg1.addView(no1);
lymn.addView(rg1);
检查所有addView
来电。 (提示:某处出现该错误的行号。使用它)
要尝试在评论中回答您的问题,您必须遵守这些规则;
findViewById
,请不要在其上使用addView
。addView
和新视图。addView
重新添加变量,则需要创建新视图。并且,我不能再强调这一点:使用错误中的行号找出,哪一行产生错误。看看你在那里添加什么,并尝试用上面的帮助找出为什么不起作用。
答案 1 :(得分:3)
这意味着您要添加一个没有它的父视图
如果您将这些按钮添加到某个视图,则LinearLayout
包含两个按钮此异常发生时您必须添加LinearLayout
(根父)而不是子子女强>