Если корзина пуста , или при повторном заполнеии корзины выводится множество ошибок исправь это : package com.example.myapp_2.Data.cart;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapp_2.R;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapp_2.R;

public class CartFragment extends Fragment implements Cart.OnCartChangedListener {

    private TextView totalPriceTextView;
    private Button placeOrderButton;
    private RecyclerView recyclerView;
    private Button backButton;

    private CartAdapter adapter;
    private Cart cart;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_cart, container, false);
        backButton = view.findViewById(R.id.back_button); // кнопка назад
        totalPriceTextView = view.findViewById(R.id.total_price_text_view);
        placeOrderButton = view.findViewById(R.id.place_order_button);
        recyclerView = view.findViewById(R.id.cart_recycler_view);

        cart = Cart.getInstance();
        cart.addOnCartChangedListener(this);

        adapter = new CartAdapter(cart.getItems(), new OnCartItemListener() {
            @Override
            public void onQuantityChanged(CartItem item, int newQuantity) {
                cart.updateItem(item, newQuantity);
            }

            @Override
            public void onRemoveButtonClick(CartItem item) {
                cart.removeItem(item);
            }
        });

        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        updateTotalPrice();

        placeOrderButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "Опять работа???", Toast.LENGTH_SHORT).show();
                getActivity().onBackPressed(); // вызов метода возврата на предыдущий экран
                cart.clear();
            }
        });
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().onBackPressed(); // вызов метода возврата на предыдущий экран
            }
        });

        return view;
    }

    private void updateTotalPrice() {
        double totalPrice = cart.getTotalPrice();
        totalPriceTextView.setText(getString(R.string.cart_item_total_price, totalPrice));
    }

    @Override
    public void onCartChanged() {
        adapter.setItems(cart.getItems());
        updateTotalPrice();
    }
}
package com.example.myapp_2.Data.cart;

import com.example.myapp_2.Data.List_1.Product;

import java.util.ArrayList;
import java.util.List;



import com.example.myapp_2.Data.List_1.Product;

import java.util.ArrayList;
import java.util.List;

public class Cart {
    private static Cart instance;
    private List<CartItem> items;
    private List<OnCartChangedListener> listeners;

    private Cart() {
        items = new ArrayList<>();
        listeners = new ArrayList<>();
    }

    public static Cart getInstance() {
        if (instance == null) {
            instance = new Cart();
        }
        return instance;
    }

    public void addItem(Product product) {
        for (CartItem item : items) {
            if (item.getProduct().equals(product)) {
                item.setQuantity(item.getQuantity() + 1);
                notifyCartChanged();
                return;
            }
        }
        items.add(new CartItem(product, 1));
        notifyCartChanged();
    }

    public void updateItem(CartItem item, int newQuantity) {
        if (newQuantity <= 0) {
            removeItem(item);
        } else {
            item.setQuantity(newQuantity);
            notifyCartChanged();
        }
    }

    public void removeItem(CartItem item) {
        items.remove(item);
        notifyCartChanged();
    }

    public List<CartItem> getItems() {
        return items;
    }

    public void clear() {
        items.clear();
        notifyCartChanged();
    }

    public double getTotalPrice() {
        double totalPrice = 0;
        for (CartItem item : items) {
            totalPrice += item.getTotalPrice();
        }
        return totalPrice;
    }

    private void notifyCartChanged() {
        for (OnCartChangedListener listener : listeners) {
            listener.onCartChanged();
        }
    }

    public void addOnCartChangedListener(OnCartChangedListener listener) {
        listeners.add(listener);
    }

    public interface OnCartChangedListener {
        void onCartChanged();
    }
}