我正在尝试创建一个可扩展的列表视图,其中每个列表的最后一项包含一个eDittext和包含文本视图的其他项目。到目前为止,我提出的代码:
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
MainActivity.ViewHolder holder = null;
final String childText = (String) getChild(groupPosition, childPosition);
boolean bLastChild = childPosition == _listDataChild.get(groupPosition).size() - 1;
if (convertView == null) {
holder = new MainActivity.ViewHolder();
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(bLastChild) {
convertView = infalInflater.inflate(R.layout.list_edit_item, null);
}else {
convertView = infalInflater.inflate(R.layout.list_item, null);
holder.textView = (TextView) convertView.findViewById(R.id.text);
}
convertView.setTag(holder);
} else {
holder = (MainActivity.ViewHolder)convertView.getTag();
}
if(!bLastChild) {
holder.textView.setText(childText);
}
return convertView;
}
现在我有以下问题:
如果我第一次有效地扩展了一个组。但是,当我崩溃并第二次扩展时,应用程序崩溃了,我会收到错误:
java.lang.nullpointerexception:尝试调用虚拟方法'void android.widget.textview.setText(java.lang.charsequence('在null上 对象参考
我猜这是因为持有人用EditText引用布局时调用holder.textView.setText(childText);
。但是我不知道为什么会发生这种情况
此代码的主要想法来自这里
编辑
登录后,我发现了,第二次扩展了小组的getchildview一次是为每个孩子调用一次,然后在ChildPosition = 0上进行一次,但是转换view与上一个ChildPosition的呼叫中的convertview保持不变:
孩子置:0;convertView:android.widget.linearlayout {A049868 V.E ......................................................................................................................................................................................................................................................................................................................... 儿童位置1;convertview:android.widget.linearlayout {6f19175 V.E ........................................................................................................................................................................................................................... 儿童位置2;convertview:android.widget.linearlayout {4aac50a v.e ........................................................................................................................................................................................................... 0,337-1080,424} 儿童位置3;convertview:android.widget.linearlayout {47b217b v.e ................................................................................................................................................................................................................................................. 儿童置换4;convertView:android.widget.linearlayout {cc2eb98 v.e ........................................................................................................................................................................................................................................................... 儿童位置5;convertView:android.widget.linearlayout {256cff1 V.E ........................................................................................................................................................................................................................................... 儿童位置6;convertView:android.widget.linearlayout {7b827d6 v.e ....................................................................................................................................................................................................... 儿童置换0;convertview:android.widget.linearlayout {7b827d6 v.e .....................................................................................................................................................................................................................................................................................................................................
通过覆盖以下两个函数来解决问题:
@Override
public int getChildTypeCount(){
return 2;
}
@Override
public int getChildType(final int groupPosition, final int childPosition){
if (childPosition == _listDataChild.get(groupPosition).size() - 1){
return 1;
}else{
return 0;
}
}