我正在尝试添加相同布局的多个视图。但是,在执行以下代码时,我得到 Exception: java.lang.IllegalStateException: The specified child has a parent。您必须首先在孩子的父母上调用 removeView()。
我究竟做错了什么?谢谢!
class GetTrackTimer extends TimerTask {
Activity act;
View RootView;
public GetTrackTimer(Activity ctx, View RootView)
{
this.act = ctx;
this.RootView = RootView;
}
@Override
public void run() {
act.runOnUiThread(new Runnable() {
@Override
public void run() {
if (TrackItems.getInstance().items == null)
return;
LinearLayout items = (LinearLayout) RootView.findViewById(R.id.itemslist);
LayoutInflater inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < TrackItems.getInstance().items.length; i++)
{
View itemView = inflater.inflate(R.layout.track_item,null);
TextView tv= (TextView)itemView.findViewById(R.id.itemName);
tv.setText(TrackItems.getInstance().items[i].Item.ID + "");
items.addView(tv);
}
}
});
}
}