【发布时间】:2014-07-26 13:51:54
【问题描述】:
我试图从 for 循环中实例化多个按钮,但我收到以下错误:
The specified child already has a parent. You must call removeView() on the child's parent first.
我尝试删除视图,但只有 1 个按钮实例化。我已经查看了具有相同错误的其他解决方案,但它们对我不起作用..任何建议都会很棒。谢谢..
这是代码
public void onClick(View v) {
speakOut("setting devices");
DeviceHelper db = new DeviceHelper(getActivity());
List<String> devs = db.getAllDevices();
int id = getResources().getIdentifier("lock", "drawable",getActivity().getPackageName());
final ImageView imageView = new ImageView(getActivity());
int amm = devs.size();
for (int i = 0; i < amm; i++) {
String nas2 = devs.get(i);
//layout.removeView(imageView);
Log.v("NAS", " db info for devices " + nas2);
imageView.setTag(ii);
imageView.setX(t + 100);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(vp);
imageView.setImageResource(id);
layout.addView(imageView);
}
【问题讨论】:
标签: java android button instantiation