1

In my code res.getDrawable(R.drawable.image_name) is deprecated. I have done some research and found this ContextCompat.getDrawable(getActivity(), R.drawable.name);

I have tried it but it is not working. Android Studio says "Cannot solve the method getActivity()" i tried to change getActivity() with this but i have an error that says "....ShoppingCartHelper Cannot be referenced from a static context"

This is my code:

public class ShoppingCartHelper {

    public static final String PRODUCT_INDEX = "PRODUCT_INDEX";

    private static List<Product> catalog;
    private static List<Product> cart;

    public static List<Product> getCatalog(Resources res){
        if(catalog == null) {
            catalog = new Vector<Product>();
            catalog.add(new Product("Dead or Alive", res.getDrawable(R.drawable.blucaffe),
                    "Dead or Alive by Tom Clancy with Grant Blackwood", 29.99));
            catalog.add(new Product("Switch", res
                    .getDrawable(R.drawable.arabic),
                    "Switch by Chip Heath and Dan Heath", 24.99));
            catalog.add(new Product("Watchmen", res
                    .getDrawable(R.drawable.espressobar),
                    "Watchmen by Alan Moore and Dave Gibbons", 14.99));
        }

        return catalog;
    }

    public static List<Product> getCart() {
        if(cart == null) {
            cart = new Vector<Product>();
        }

        return cart;
    }

}

I don't understand why it is not working. I have tried many other code snippets but it is always giving me errors.

Aryan
  • 11
  • 6
  • You must pass the context to your class so add new Context attribute and pass it when you instantiate the class with constructor then use it, for exemple : ContextCompat.getDrawable(ctx, R.drawable.name); – Amine Harbaoui Sep 28 '17 at 13:41
  • @AmineHarbaoui thanks a lot, but i am new to android could you explain better please ? – Aryan Sep 28 '17 at 14:31

0 Answers0