问题描述
我的code是这里
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();
}
}
当我运行此code igot错误这样
when i run this code igot error like this
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帮助我在这
推荐答案
您收到此错误是很难找到的,因为你的code是所有的地方,但要添加的错误意味着一个查看
(孩子)这是媒体链接在屏幕上(有父)。
The error you are getting is hard to find because your code is all over the place, but the error means you are adding a View
(child) that is allready on the screen (has a parent).
这可以在任何地方,很难与这个帖子的说法,但例如:
This could be anywhere, hard to say with this post, but for example:
这一次大概如下:
EditText ed = new EditText(this);
ed.setWidth(250);
lymn.addView(ed);
ED
是新的,所以没有一个家长。
ed
is new, so doesn't have a parent.
但我找不到 YES1
的声明,所以这一块可能是culprint。或者,也许 NO1
。
But I can't find the declaration of yes1
, so this one MIGHT be the culprint. Or maybe no1
.
yes1.setText("Yes");
no1.setText("NO");
rg1.addView(yes1);
rg1.addView(no1);
lymn.addView(rg1);
检查所有的 addView
通话。 (提示:存在这样的错误某处行号使用)
Check all your addView
calls. (hint: there is a line-number in that error somewhere. use it)
要试图回答的评论你的问题,你必须遵循这些规则;
To try to answer your question in the comment, you must follow these rules;
- 从不添加任何视图超过一次。
- 当视图使用媒体链接(例如,你得到了它
findViewById
,不使用addView
上吧。 - 当你想添加视图,使用
addView
有一个新的看法。 - 您可以添加多个这些新的观点有一种意见认为,但你不能添加一个视图多次。
- 您不能简单地通过改变一些东西,重新使用视图。你可以重新使用一个变量,但你需要一个新的观点,如果你想使用
addView
重新添加。
- Never add any view more then once.
- When a View is allready used (e.g., you got it with
findViewById
, don't useaddView
on it. - When you want to add a view, use
addView
with a NEW view. - You can add several of these new views to one view, but you cannot add that one view multiple times.
- You can't re-use a view simply by changing some stuff. You CAN re-use a variable, but you need to make a new view if you want to re-add it using
addView
.
和,我不能强调这更多:找出,使用错误,这行产生错误的行号。看看您要添加那里的东西,揣摩上面的帮助,为什么不工作。
And, I can't stress this more: find out, using the line-number in the error, which line produces the error. Look at what you are adding there, and try to figure out with above help why that doesn't work.
这篇关于指定的孩子已经有父的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!