指定的孩子已经有父母 [英] the specified child already has a parent

查看:27
本文介绍了指定的孩子已经有父母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码在这里

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(); 
    }
}

当我运行此代码时,出现了这样的错误

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)

请帮我解决这个问题

推荐答案

你得到的错误很难找到,因为你的代码到处都是,但错误意味着你正在添加一个View(孩子)已经出现在屏幕上(有父母).

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的声明,所以这个可能是罪魁祸首.或者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;

  • 切勿多次添加任何视图.
  • 当一个 View 已经被使用时(例如,你用 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 use addView 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.

这篇关于指定的孩子已经有父母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆