我想膨胀一个childView of ExpandableChildView组件。
代码:
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View v = convertView;
LinearLayout linearOpt = themesOptions.get(childPosition);
if(v == null) {
v = mInflater.inflate(R.layout.itemrow, null);
}
LinearLayout ll = (LinearLayout) v.findViewById(R.id.root);
ll.addView(linearOpt);
return v;
}其中linearOpt是包含大量LinearLayout对象的向量,我已经实例化了这些对象。
private void prepareThemes(){
View v = mInflater.inflate(R.layout.widget_configure, null);
LinearLayout theme = (LinearLayout) v.findViewById(R.id.themeLayout);
LinearLayout color = (LinearLayout) v.findViewById(R.id.colorLayout);
LinearLayout trans = (LinearLayout) v.findViewById(R.id.transpLayout);
themesOptions.add(theme);
themesOptions.add(color);
themesOptions.add(trans);
}这是R.layout.itemrow xml:
但是我收到了这个错误:
07-18 10:48:49.740: ERROR/AndroidRuntime(2738):java.lang.IllegalStateException:指定的子程序已经有一个父级。您必须首先对孩子的父级调用removeView()。
我怎样才能解决这个问题?
发布于 2010-08-08 23:20:06
我认为问题是由您在prepareThemes()中准备布局的方式造成的。
您没有提到,但我假设您的‘layout/widget_figre.xml’定义了如下结构:?
<LinearLayout android:id="@+id/root">
<LinearLayout android:id="@+id/themeLayout> <!-- ... --> </LinearLayout>
<LinearLayout android:id="@+id/colorLayout> <!-- ... --> </LinearLayout>
<LinearLayout android:id="@+id/transpLayout> <!-- ... --> </LinearLayout>
</LinearLayout>然后在prepareThemes()中充气,得到3个子布局。但此时,它们已经注册为周围布局的子程序(我称之为“根”)。正如错误所暗示的,视图实例只能添加到一个父中。
您可以将每个LinearLayout存储在自己的xml文件中,然后膨胀3次。然后,这些存储的布局可以添加一次给孩子。
我不知道你想做什么,但我想,最好是--而不是准备--只需要3种不同的布局,其中包括layout.itemrow的部分,然后在getChildView()中制作一个switch case,然后动态地膨胀所需的布局。因为,即使在prepareThemes中存储非绑定布局时:一旦有多个组,将预存储的布局添加到snd组的子组时,也会得到相同的错误。
我希望这能帮到你。
https://stackoverflow.com/questions/3275204
复制相似问题