0

The code says that clicking on cart image the name of product becomes red.This works for the first items that appears in listview(only items that you can see on phone display).

Scrolling down to see other items in listview it works but when i click on item to add it to cart the application crashes at line : parent.getChildAt(position).findViewById(R.id.textViewNameChangeColor).setVisibility(View.VISIBLE);

with NullPointerException

I don't see what could cause the problem and some help it would be appriciated.Thanks.

@Override
    public int getCount() {
        return listTransferObjectProduct.size();
    }

    @Override
    public Object getItem(int position) {
        return listTransferObjectProduct.get(position);
    }

    @Override
    public long getItemId(int position) {
        return  position;
    }

    private class Holder {
        TextView textViewProduct;
        TextView textViewNameChangeColor;
        ImageView addOneItem;
        TextView textViewPrice;
    }

    Holder holder;

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {

        product = listTransferObjectProduct.get(position);
        holder = new Holder();
        convertView = inflater.inflate(R.layout.custom_product_adapter, null);

        holder.textViewProduct = (TextView) convertView.findViewById(R.id.textViewName);
        holder.textViewPrice = (TextView) convertView.findViewById(R.id.textViewPrice);
        holder.textViewNameChangeColor =(TextView) convertView.findViewById(R.id.textViewNameChangeColor);
        holder.textViewNameChangeColor.setVisibility(View.INVISIBLE);
        holder.addOneItem = (ImageView) convertView.findViewById(R.id.imageViewAdd);

        holder.textViewProduct.setText(product.articleName);
        holder.textViewNameChangeColor.setText(product.articleName);
        holder.textViewPrice.setText(Double.toString(listTransferObjectProduct.get(position).price) + " $");


        holder.addOneItem.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction()){

                    case MotionEvent.ACTION_DOWN:

                        utility.incrementCounter((Activity) context);
                        product = listTransferObjectProduct.get(position);
                        product.quantity = 1;
                        parent.getChildAt(position).findViewById(R.id.textViewNameChangeColor).setVisibility(View.VISIBLE);
                        utility.saveToSharedPrefereces(product, true);
                        return true;

                    case MotionEvent.ACTION_UP:

                        parent.getChildAt(position).findViewById(R.id.textViewNameChangeColor).setVisibility(View.INVISIBLE);
                        return true;
                }
                return false;
            }
        });
        return convertView;
    }
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
John
  • 17
  • 7
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – AskNilesh Dec 06 '17 at 11:30
  • try to get view's object in `onTouch()` method `v.getParent` object then do your process. – Dharmishtha Dec 06 '17 at 11:52

0 Answers0