Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
366 views
in Technique[技术] by (71.8m points)

android - the specified child already has a parent

my code is here

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)

plz help me on this

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

This one probably works:

  EditText ed = new EditText(this);
  ed.setWidth(250);
  lymn.addView(ed);

ed is new, so doesn't have a parent.

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

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;

  • 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...