Java – creating table rows using loops does not work

I create a code to programmatically add lines to 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 works well. But I tried to 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 does not run. How can I solve it? This is an 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) 

resolvent:

You create variables outside the loop, edit the same row at each iteration, and then add them to the list, which results in an exception, because the row view has been added and tells you that you need to call removeView. before adding it again.

Just initialize the variable within the loop and move the line within 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 assume it is also a view. The same fact applies to it. You can't add the same view. For multiple parent objects, you need to create a new object every time, so your loop should be as follows

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>