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

java-使用循环创建表行不起作用,第1张

概述我创建一个代码以编程方式在xml中添加行,这是代码://layout/*createalinearlayout*/LinearLayoutlinearUserPicture=newLinearLayout(this);//linear.setGravity(Gravity);linearUserPicture.setOrientation(LinearLayout.VERTICAL);LinearLayo

我创建一个代码以编程方式在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.access0(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.access0(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) 

解决方法:

您在循环外创建了变量,每次迭代都编辑同一行,然后将其添加到列表中,这会导致异常,因为该行视图已经添加,并告诉您需要在再次添加之前调用removeVIEw.

只需在循环内初始化变量
在循环内移动这条线

tableRow tr = new tableRow(this);

编辑:
相同的事实适用于androID系统中的任何视图,您没有说变量“ linearUserPicture”是什么,我假设它也是一个视图,相同的事实适用于它,您不能添加相同的视图对于多个父对象,您每次都需要为其创建一个新对象,因此您的循环应如下所示

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-使用循环创建表行不起作用全部内容,希望文章能够帮你解决java-使用循环创建表行不起作用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1120614.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存