我正在尝试将两种不同的布局加载到我的可扩展列表 child 中。我一直得到一个空指针,这听起来像我!=0
实际上并没有按照我的意图工作。我做错了什么?
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
Log.e("COMMENTS", "Ok so the POSITION: " + childPosition);
if (convertView == null) {
if(childPosition==0){
//first view
convertView = inflater.inflate(R.layout.comments_create_comment, null);
}else {
//second view
convertView = inflater.inflate(R.layout.comments_expandable_list_child, null);
}
}
if(childPosition!=0){
TextView item = (TextView) convertView.findViewById(R.id.comments_expandable_list_child_text_view);
ImageView delete = (ImageView) convertView.findViewById(R.id.comments_expandable_list_child_delete);
//throwing a null pointer exception at the delete.setonclick
//I'm guessing it's still using the first view here somehow?
delete.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
List<String> child = comments_feed_collection.get(group_list.get(groupPosition));
child.remove(childPosition);
notifyDataSetChanged();
}
});
item.setText(laptop);
}
return convertView;
}
准确打印出来:
04-22 10:27:58.762 21013-21013/com.myapp E/COMMENTS﹕ Ok so the POSITION: 0
04-22 10:27:58.772 21013-21013/com.myapp I/Editor﹕ setup window support handles
04-22 10:27:58.772 21013-21013/com.myapp E/COMMENTS﹕ Ok so the POSITION: 1
04-22 10:27:58.772 21013-21013/com.myapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4183fda0)
04-22 10:27:58.792 21013-21013/com.myapp E/oh nooo﹕ java.lang.NullPointerException
at comments.CommentsExpandableListAdapter.getChildView(CommentsExpandableListAdapter.java:58)
答案 0 :(得分:4)
你的问题在于:
if (convertView == null) {
if(childPosition==0){
//first view
convertView = inflater.inflate(R.layout.comments_create_comment, null);
}else {
//second view
convertView = inflater.inflate(R.layout.comments_expandable_list_child, null);
}
}
convertView
为空getChildTypeCount
次。如果您没有覆盖此方法,则convertView
只会膨胀一次。最有可能是你的childPosition==0
案例。您可以找到here文档