我正在尝试在Exapandable ListView组项中使用两个视图,当我单击其中一个项目进行扩展时,它给了我NPE。当我选择一种类型的组项时,它不会发生。
NPE
getChildView()方法中的txtListChild.setText(“”+ childText)行
QuestionListViewAdapter.java
public class QuestionListViewAdapter extends BaseExpandableListAdapter{
private Context _context;
private List<Question> _listDataHeader;
private HashMap<String, List<String>> _listDataChild;
public QuestionListViewAdapter(Context context, List<Question> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View childView;
if (convertView == null) {
LayoutInflater infalInflater = LayoutInflater.from(_context);
childView = infalInflater.inflate(R.layout.question_list_item_type_selection_option, null);
}else{
childView = convertView;
}
final String childText = getChild(groupPosition,childPosition).toString();
TextView txtListChild = (TextView) childView
.findViewById(R.id.textView_question_list_item_type_selection_option_text);
txtListChild.setText(""+childText);
return childView;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return _listDataChild.get(_listDataHeader.get(groupPosition).get("title")).get(childPosititon);
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition).get("title"))
.size();
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public int getGroupType(int groupPosition) {
return Integer.valueOf(_listDataHeader.get(groupPosition).get("type").toString());
}
@Override
public int getGroupTypeCount() {
return 2;
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition).get("title").toString();
}
@Override
public int getGroupCount() {
return _listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch (getGroupType(groupPosition)) {
case 1:convertView = infalInflater.inflate(R.layout.question_list_item_type_selection, null);
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.textView_Question_List_Item_Type_Selection_Question);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
break;
case 2:convertView = infalInflater.inflate(R.layout.question_list_item_type_input_text, null);
EditText et_lblListHeader = (EditText)convertView.findViewById(R.id.editText_Question_List_Item_Type_Input_Text_Question);
//et_lblListHeader.setTypeface(BootstrapApplication.getInstance().getTypeface(1));
et_lblListHeader.setHint(headerTitle);
break;
}
}
return convertView;
}
}
question_list_item_type_selection_option.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView_question_list_item_type_selection_option_text"
android:layout_margin="5dp" />
</LinearLayout>