尝试在 CreateView 上的片段中膨胀第二个布局时出错



我正在尝试在我的onCreateView中膨胀视图,但我不断收到此错误:

The specified child already has a parent. You must call removeView() on the child's parent first.

我正在尝试膨胀视图并根据列表中的项目数量将视图多次添加到指定的线性布局中。我在这里做错了什么?

View v = View.inflate(getActivity(), R.layout.list_item, null);
        for(int i = 0; i < totalItems; i++) {
            TextView itemNumbers = (TextView) v.findViewById(R.id.itemNumbers);
            ImageView itemBubbles = (ImageView) v.findViewById(R.id.itemBubbles);
            itemNumbers.setText("0" + String.valueOf(i+1));
            itemBubbles.setTag(i);
            final int finalI = i;
            itemBubbles.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setupValues(finalI);
                }
            });
            theListItems.addView(v, i);
        }

你的通货膨胀需要在循环中。 你正在给它充气一次,然后尝试多次添加它。 第二次它已经被添加,所以它失败了。 每个项目都需要单独充气。