java - 使用循环创建表行不起作用

标签 java android

我创建了一个代码以在我的 xml 中以编程方式添加行,这是代码:

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

代码运行正常。 但是我试图测试创建很多行,所以我添加了一个循环来测试:

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

代码不运行。我该如何解决?这是错误:

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) 

最佳答案

你在循环外创建了你的变量,每次迭代你都在编辑同一行然后将它添加到列表中,这会导致异常,因为行 View 已经添加,它告诉你你需要在你之前调用 removeView再补充一下

只需在循环内初始化变量 将此行移到循环内

TableRow tr = new TableRow(this);

编辑: 同样的事实适用于android系统中的任何 View ,你没有说变量“linearUserPicture”是什么,我假设它也是一个 View ,同样的事实适用于它,你不能添加相同的 View 对于不止一个父对象,你每次都需要为它创建一个新对象,所以你的循环应该是这样的

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

关于java - 使用循环创建表行不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35435276/

相关文章:

java - 如何创建多层JComboBox

java - 异步任务 : invalidating view does not take effect

java.lang.ClassCastException : android. support.constraint.ConstraintLayout 无法转换为 android.widget.TextView

java - Android 堆转储转换为 J2SE : hprof-conv: command not found

java - 如何获得要打印的总页数?

java - 无法将 Hadoop 2.2 的虚拟内存更改为超过默认的 2.1 GB

java - PDFBox 2.0.7 ExtractText 不工作但 1.8.13 和 PDFReader 也一样

java - EasyMock 返回 null

android - 如何将手机变成投影仪,将手机屏幕上显示的内容转换到墙上

android - 在Facebook和Youtube上同时直播视频