Alexandre C. do Carmo
Alexandre C. do Carmo

Reputation: 701

Create tablerow using loop dont work

I create a code to add row programmatically in my xml, this is the code:

//layout
     /*create a linear layout */
    LinearLayout linearUserPicture = new LinearLayout(this);
    //linear.setGravity(Gravity);
    linearUserPicture.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            wlienar, hlienar);

    layoutParams.setMargins(0, 0, margim, 0);
    layoutParams.gravity = Gravity.NO_GRAVITY;


    RoundedImageView imageView = new RoundedImageView(this, null);
    imageView.setImageResource(R.mipmap.eu);

    //setting image position
    int w = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());

    // Log.d("divisaoxxxx", String.valueOf(w));
    imageView.setLayoutParams(new ViewGroup.MarginLayoutParams(w, w));

    linearUserPicture.addView(imageView, layoutParams);

    //adicionar itens na tabela
    /* Find Tablelayout defined in main.xml */
    TableLayout tl = (TableLayout) findViewById(R.id.feeds_table);
    /* Create a new row to be added. */
    TableRow tr = new TableRow(this);
    for(int i=0 ; i < 2 ; i++) {



        tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        tr.setBackgroundResource(R.drawable.border);


        tr.addView(linearUserPicture);

    }
    /* Add row to TableLayout. */
    tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));

The code worked ok. But I trying test to create many lines, so I added a loop to test:

 TableLayout tl = (TableLayout) findViewById(R.id.feeds_table);
    /* Create a new row to be added. */
    TableRow tr = new TableRow(this);
    for(int i=0 ; i < 2 ; i++) {



        tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        tr.setBackgroundResource(R.drawable.border);


        tr.addView(linearUserPicture);

    }
    /* Add row to TableLayout. */
    tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));

The code dont run. How can I fix it? This is the error:

02-16 12:25:14.911 20245-20245/com.example.alexandre_pc.beerin E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.alexandre_pc.beerin/com.example.alexandre_pc.beerin.FeedsActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
 at android.app.ActivityThread.access$700(ActivityThread.java:140)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:137)
 at android.app.ActivityThread.main(ActivityThread.java:4921)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:511)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
 at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
 at android.view.ViewGroup.addViewInner(ViewGroup.java:3620)
 at android.view.ViewGroup.addView(ViewGroup.java:3491)
 at android.view.ViewGroup.addView(ViewGroup.java:3436)
 at android.view.ViewGroup.addView(ViewGroup.java:3412)
 at com.example.alexandre_pc.beerin.FeedsActivity.onCreate(FeedsActivity.java:86)
 at android.app.Activity.performCreate(Activity.java:5206)
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135) 
 at android.app.ActivityThread.access$700(ActivityThread.java:140) 
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237) 
 at android.os.Handler.dispatchMessage(Handler.java:99) 
 at android.os.Looper.loop(Looper.java:137) 
 at android.app.ActivityThread.main(ActivityThread.java:4921) 
 at java.lang.reflect.Method.invokeNative(Native Method) 
 at java.lang.reflect.Method.invoke(Method.java:511) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) 
 at dalvik.system.NativeStart.main(Native Method) 

Upvotes: 3

Views: 71

Answers (1)

Hassan Khallouf
Hassan Khallouf

Reputation: 1178

You made your variable outside the loop, each iteration you are editing the same row and then add it to the list, which causes an exception because the row view is already added, and it's telling you you need to call removeView before you add it again

Just initialize your variable inside the loop move this line inside the loop

TableRow tr = new TableRow(this);

EDIT: The same fact applies to any view in the android system, you didn't say what the variable "linearUserPicture" is, I'm assuming it's a view as well, the same fact applies to it , you can't add the same view to more than one parent , you need to create a new object for it each time , so your loop should be like this

for(int i=0 ; i < 2 ; i++) {
TableRow tr = new TableRow(this);



    tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
    tr.setBackgroundResource(R.drawable.border);

linearUserPicture = new View(); //DON'T COPY THIS! re create your view as you did it before every time
    tr.addView(linearUserPicture); // INITIALIZE linearUserPicture every loop!
tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
}

Upvotes: 3

Related Questions