我创建了一个代码,用于在我的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)
答案 0 :(得分:3)
你使你的变量在循环之外,你正在编辑同一行的每次迭代然后将它添加到列表中,这会导致异常,因为行视图已经添加,它告诉你需要在你之前调用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));
}