ExpandableListView 更改第一个 child 的布局

ExpandableListView Change layout of first child

我正在尝试将两种不同的布局加载到我的可扩展列表中 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)

您的问题出在这里:

 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 为 null getChildTypeCount 次。如果你没有重写这个方法,convertView 只会膨胀一次。很可能是针对您的 childPosition==0 案例。您可以找到 here 文档