2

I am gettin NullPointerException on line:

FragmentManager fragmentManager = ((ProductActivity)context).getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();

The context is returning null and when I click to replace the fragment in my adapter below, it's crashed.

Here's my adapter:

public class ProductActivityAdapter extends RecyclerView.Adapter<ProductActivityAdapter.ViewHolder> {

        private List<Product> products;
        private int rowLayout;
        public Context mContext;
        public  int position;

        public ProductActivityAdapter(List<Product> products, int rowLayout, Context context) {
        this.products = products;
        this.rowLayout = rowLayout;
        this.mContext = context;
            }

        @Override
            public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
                View v = LayoutInflater.from(viewGroup.getContext()).inflate(rowLayout, viewGroup, false);
        return new ViewHolder(v);
            }


        @Override
            public void onBindViewHolder(ViewHolder viewHolder, int i) {
                Product product = products.get(i);
                viewHolder.card.setTag(i);
                viewHolder.countryName.setText(product.name);
        if(android.os.Build.VERSION.SDK_INT >= 21){
        //  viewHolder.countryImage.setImageDrawable(mContext.getDrawable(product.getImageResourceId(mContext)));
                    viewHolder.iconImage.setImageDrawable(mContext.getDrawable(product.getIconImage(mContext)));
                } else {
        //  viewHolder.countryImage.setImageDrawable(mContext.getResources().getDrawable(product.getImageResourceId(mContext)));
                    viewHolder.iconImage.setImageDrawable(mContext.getResources().getDrawable(product.getIconImage(mContext)));


                }

            }

        @Override
            public int getItemCount() {
        return products == null ? 0 : products.size();
            }

        public static class ViewHolder extends RecyclerView.ViewHolder  {
        public TextView countryName;
        public ImageView countryImage;
        public ImageView iconImage;
        public CardView card;
        public Context context;

        public Context getContext() {
        return context;
                    }
            public ViewHolder(View itemView) {
        super(itemView);
        countryName = (TextView) itemView.findViewById(R.id.countryName);
        // countryImage = (ImageView)itemView.findViewById(R.id.countryImage);
                    iconImage = (ImageView) itemView.findViewById(R.id.iconImage);
        card = (CardView) itemView.findViewById(R.id.card);
        card.setOnClickListener(new CardClickListener());

                }
        private class CardClickListener implements CardView.OnClickListener {

        @Override
                        public void onClick(View v) {
        //  Log.e("VIEW", "position=" + position);
                            int position = (int) v.getTag();
                            Log.e("VIEW", "position=" + position);
                            cardview(position);

                        }
                    }

        public void cardview(int position) {
                    Fragment fragment = null;
        switch (position) {
        case 0:
                            fragment = new emi_calculator_fragment();
        break;
        case 1:
                            Log.e("Position", "HI");
        break;
        case 2:
                            Log.e("Position", "HI");
        break;
        case 3:
                            Log.e("Position", "HI");
        break;
        case 4:
                            Log.e("Position", "HI");
        break;
        case 5:
                            Log.e("Position", "HI");
        break;

        default:
        break;
                    }
        if (fragment != null) {

                        FragmentManager fragmentManager = ((ProductActivity)context).getSupportFragmentManager();
                        fragmentManager.beginTransaction()
                                .replace(R.id.frame_container, fragment).commit();

                    } else {
        // error in creating fragment
                        Log.e("Product Activity", "Error in creating fragment");
                    }
                }   
            }
            }
Pankaj
  • 7,908
  • 6
  • 42
  • 65
user5397448
  • 243
  • 4
  • 17

2 Answers2

2

Use

mContext.getSupportFragmentManager();

instead of

((ProductActivity)context).getSupportFragmentManager();

As you have defined Context varaible as mContext.

Pankaj
  • 7,908
  • 6
  • 42
  • 65
1

In the ViewHolder class you didn't set the context, so it is null.

public ViewHolder(Context context, View itemView) {
  super(itemView);
  this.context = context;
  ...
}
Milos Fec
  • 828
  • 6
  • 11
  • 1
    @Clairvoyant I cannot comment your answer yet... ViewHolder is static, so there is no access to mContext. – Milos Fec Dec 18 '15 at 10:46