Developer
Developer

Reputation: 1813

android ExpandableListView - NullPointerException

Blockquote

I'm using ExpandableListView in android

this is the code of getChildView in the adapter class:

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
         ViewHolder holder; 
        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) myContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.childrow, null);
            holder = new ViewHolder(); 

            holder.tv = (TextView) convertView
                    .findViewById(R.id.textView1);
                            // I added this line of code
                holder.stop = (Button) convertView.findViewById(R.id.button3);

            holder.position = groupPosition;
            convertView.setTag(holder);
            holder.stop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

               ViewHolder holder = new ViewHolder();
               holder = (ViewHolder) v.getTag();

                    int position = 0;

                        position = holder.position; ************* here

                    System.out.println("coolapse tag " + position);

                    expList.collapseGroup(position);


                }
            });

        } else {
             holder = (ViewHolder) convertView.getTag();
        }

        return convertView;
    }

It gives NullPointerException in the line marked by ** what is the problem ?

Upvotes: 1

Views: 823

Answers (1)

dmon
dmon

Reputation: 30168

What is holder.stop? holder.stop is NOT the view where you set the tag, so when you try to do v.getTag(), you get a null in holder (incidentally, I did not see holder.stop being initialized). Not sure exactly what you're going for, but a way to fix it would be to set the holder on the stop view as well:

convertView.setTag(holder);
holder.stop.setTag(holder);
holder.stop.setOnClickListener(....

Upvotes: 1

Related Questions