ekj*_*yot 1 android tablelayout
我正在制作一个Android应用程序,我在其中动态制作表格行,然后动态添加图像视图和文本视图.它添加第一行,然后给出以下异常
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Run Code Online (Sandbox Code Playgroud)
我正在使用以下代码:
ar1= new JSONArray(children.toString());
for(int mn=0;mn<ar1.length();mn++){
value=ar1.getString(mn);
TableRow tr = new TableRow(HomePageWithPhoneIsOfflineDialog.this);
tr.setBackgroundColor(Color.WHITE);
tr.setId(100+mn);
tr.setMinimumHeight(60);
tr.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TextView child= new TextView(HomePageWithPhoneIsOfflineDialog.this);
child.setId(200+mn);
child.setHeight(50);
child.setGravity(Gravity.CENTER);
child.setTextColor(Color.BLACK);
System.out.println(value);
child.setText(value);
System.out.println("adding iv");
tr.addView(iv,0);
System.out.println("adding child");
tr.addView(child);
System.out.println("adding table row");
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
Run Code Online (Sandbox Code Playgroud)
}
谁能告诉我如何解决这个问题.
谢谢
tr.addView(iv,0);
Run Code Online (Sandbox Code Playgroud)
问题在于ImageView(iv),我想,因为它没有在循环中实例化,所以第一次将它添加到一行,当你再次将它添加到另一行时,它不允许,因为一个视图可以只有一个父母.
所以我建议你每次循环时都要创建一个新的ImageView.
| 归档时间: |
|
| 查看次数: |
2569 次 |
| 最近记录: |