1

The following code shows RecyclerView Class and underlying activity is Tab layout. Right now the way code is I am getting an error where I declare the AlertDialog the error says Builder cannot be applied to the Class if I use Context the app crashes. I have used the same code before but with Firestore Recycler Adapter and TabLayout. This question is not a duplicate, null pointer java/lang/nullpointerexception is too general. I have gone through all of them.

public class RecyclerAdapter extends FirestoreRecyclerAdapter<Items, 
RecyclerAdapter.MyViewHolder> {

Context context;


public RecyclerAdapter(@NonNull FirestoreRecyclerOptions<Items> options) 
{
    super(options);
}

@Override
protected void onBindViewHolder(@NonNull MyViewHolder holder, final int 
position, @NonNull final Items model) {
    holder.name.setText(model.getName());
    holder.category.setText(model.getCategory());
    holder.price.setText(model.getPrice());
    holder.manu.setText(model.getManufacturer());


    Picasso.get()
            .load(model.getImage())
            .fit()
            .centerCrop()
            .into(holder.Thumbnail);


    holder.addtocart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showAddToCartDialog(model);
        }
    });
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int 
 viewType) {
    View view;
    LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
    view = mInflater.inflate(R.layout.activity_adapter, parent, false);
    return new MyViewHolder(view);
}

class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView name;
    public TextView category;
    public ImageView Thumbnail;
    public TextView price;
    public TextView manu;
    public Button addtocart;

    public MyViewHolder(View itemView) {
        super(itemView);
        name = itemView.findViewById(R.id.rowname);
        category = itemView.findViewById(R.id.categorie);
        Thumbnail = itemView.findViewById(R.id.thumbnail);
        price = itemView.findViewById(R.id.price);
        manu = itemView.findViewById(R.id.manu);
        addtocart = itemView.findViewById(R.id.cartbutton);
    }
}

public void showAddToCartDialog(final Items items) {
    View view;

    AlertDialog.Builder builder = new 
    AlertDialog.Builder(RecyclerAdapter.this);
    LayoutInflater lflator = LayoutInflater.from(context);
    view = lflator.inflate(R.layout.add_to_cart_layout, null);

    ImageView image_product_dialogue = 
    view.findViewById(R.id.image_cart_product);
    TextView textView_product_dialogue = 
    view.findViewById(R.id.text_cart_product);
    RadioButton nairobi = view.findViewById(R.id.radio_nairobi);
    RadioButton mombasa = view.findViewById(R.id.radio_mombasa);
    RadioButton kisumu = view.findViewById(R.id.radio_kisumu);
    RadioButton nakuru = view.findViewById(R.id.radio_nakuru);
    RadioButton express = view.findViewById(R.id.radio_ex);
    RadioButton tomorow = view.findViewById(R.id.radio_tomo);
    final ElegantNumberButton text_count = 
    view.findViewById(R.id.text_count);

    express.setOnCheckedChangeListener(new 
    CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean 
        isChecked) {
            if (isChecked) {
                Items.delivery = 0;
            }
          }
      });

    tomorow.setOnCheckedChangeListener(new 
    CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean 
         isChecked) {
            if (isChecked)
                Items.delivery = 1;

        }
    });

    nairobi.setOnCheckedChangeListener(new 
    CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean 
   isChecked) {
            if (isChecked)
                Items.location = 0;

        }
    });
    mombasa.setOnCheckedChangeListener(new 
    CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean 
   isChecked) {
            if (isChecked)
                Items.location = 1;

        }
    });
    kisumu.setOnCheckedChangeListener(new 
    CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean 
  isChecked) {
            if (isChecked)
                Items.location = 2;

        }
    });
    nakuru.setOnCheckedChangeListener(new 
    CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean 
   isChecked) {
            if (isChecked)
                Items.location = 3;

        }
    });

    Picasso.get()
            .load(items.getImage())
            .fit()
            .centerCrop()
            .into(image_product_dialogue);
    textView_product_dialogue.setText(items.getName());

    builder.setView(view);

    builder.setNegativeButton("ADD TO CART", new 
    DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            // showConfirmDialogue(position, text_count.getNumber(), 
   text_count.getNumber());
            dialog.dismiss();

        }
    });


    builder.show();

  }

 }
  • @Stultuske No, that's too general. –  May 24 '19 at 08:50
  • there is but one cause for a NPE, and it's explained in that link. You show us no stacktrace, you don't tell on which line the exception is thrown ...it's not as if we can run your code here. If you don't understand what is explained in that link, maybe you should focus on the basics before trying to create apps – Stultuske May 24 '19 at 08:52
  • @Stultuske it doesn't throw anything, it can't compile it shows `Builder (android.content.Context) in Builder cannot be applied to (com.example.navigationdrawer.RecyclerAdapter)  ` under `AlertDialog.Builder builder = new AlertDialog.Builder(RecyclerAdapter.this)` It only throws the null pointer error if I use Context. –  May 24 '19 at 08:57
  • why are you trying to open dialog from the adapter? – Jainil Patel May 24 '19 at 09:09
  • @JainilPatel a good question, the reason I did it is that it usually works for me except today. Suggestions will be appreciated. –  May 24 '19 at 09:15
  • well try to add interface in your adapter and implement in activiy and pass that implementation in your adapter like this class Activity():InterFaceDialog {}, class adapter(val interfaceDialog:InterFaceDialog), while creating adapter = adapter(this@Activity), interFaceDialog(){ fun doClick(position:Int) } – Jainil Patel May 24 '19 at 09:19
  • "But Dialogue keep throwing Null pointer error" vs "it doesn't throw anything". Either it throws an NPE, or it doesn't – Stultuske May 24 '19 at 09:22
  • @Stultuske let me edit that –  May 24 '19 at 09:24

3 Answers3

1

Pass activity context in the constructor and use that context to create Dialog. something like..

public RecyclerAdapter(@NonNull FirestoreRecyclerOptions<Items> options,Activity context) 
{
    super(options);
    this.context=context;
}
kelvin
  • 1,480
  • 1
  • 14
  • 28
0

Check this answer (same problem as yours) Android | AlertDialog on RecyclerView Item Click

As mentioned in other answer you can pass context to Adapter constructor, my personal opinion it is better not pass activity context that way (some memory leaks may appear), so other way is to create ClickListener interface and pass this event (possible with data as method parameter) to activity/fragment where you will handle the click and create the AlertDialog.

mr.kostua
  • 696
  • 6
  • 12
0

Just replace your Adapter class code with this -

public class RecyclerAdapter extends FirestoreRecyclerAdapter<Items, 
RecyclerAdapter.MyViewHolder> {

Context context;


public RecyclerAdapter(@NonNull FirestoreRecyclerOptions<Items> options, Context context) 
{
    super(options);
    this.context = context;
}

@Override
protected void onBindViewHolder(@NonNull MyViewHolder holder, final int 
position, @NonNull final Items model) {
    holder.name.setText(model.getName());
    holder.category.setText(model.getCategory());
    holder.price.setText(model.getPrice());
    holder.manu.setText(model.getManufacturer());


    Picasso.get()
            .load(model.getImage())
            .fit()
            .centerCrop()
            .into(holder.Thumbnail);


    holder.addtocart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showAddToCartDialog(model);
        }
    });
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int 
 viewType) {
    View view;
    LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
    view = mInflater.inflate(R.layout.activity_adapter, parent, false);
    return new MyViewHolder(view);
}

class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView name;
    public TextView category;
    public ImageView Thumbnail;
    public TextView price;
    public TextView manu;
    public Button addtocart;

    public MyViewHolder(View itemView) {
        super(itemView);
        name = itemView.findViewById(R.id.rowname);
        category = itemView.findViewById(R.id.categorie);
        Thumbnail = itemView.findViewById(R.id.thumbnail);
        price = itemView.findViewById(R.id.price);
        manu = itemView.findViewById(R.id.manu);
        addtocart = itemView.findViewById(R.id.cartbutton);
    }
}

public void showAddToCartDialog(final Items items) {
    View view;

    AlertDialog.Builder builder = new 
    AlertDialog.Builder(context);
    LayoutInflater lflator = LayoutInflater.from(context);
    view = lflator.inflate(R.layout.add_to_cart_layout, null);

    ImageView image_product_dialogue = 
    view.findViewById(R.id.image_cart_product);
    TextView textView_product_dialogue = 
    view.findViewById(R.id.text_cart_product);
    RadioButton nairobi = view.findViewById(R.id.radio_nairobi);
    RadioButton mombasa = view.findViewById(R.id.radio_mombasa);
    RadioButton kisumu = view.findViewById(R.id.radio_kisumu);
    RadioButton nakuru = view.findViewById(R.id.radio_nakuru);
    RadioButton express = view.findViewById(R.id.radio_ex);
    RadioButton tomorow = view.findViewById(R.id.radio_tomo);
    final ElegantNumberButton text_count = 
    view.findViewById(R.id.text_count);

    express.setOnCheckedChangeListener(new 
    CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean 
        isChecked) {
            if (isChecked) {
                Items.delivery = 0;
            }
          }
      });

    tomorow.setOnCheckedChangeListener(new 
    CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean 
         isChecked) {
            if (isChecked)
                Items.delivery = 1;

        }
    });

    nairobi.setOnCheckedChangeListener(new 
    CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean 
   isChecked) {
            if (isChecked)
                Items.location = 0;

        }
    });
    mombasa.setOnCheckedChangeListener(new 
    CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean 
   isChecked) {
            if (isChecked)
                Items.location = 1;

        }
    });
    kisumu.setOnCheckedChangeListener(new 
    CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean 
  isChecked) {
            if (isChecked)
                Items.location = 2;

        }
    });
    nakuru.setOnCheckedChangeListener(new 
    CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean 
   isChecked) {
            if (isChecked)
                Items.location = 3;

        }
    });

    Picasso.get()
            .load(items.getImage())
            .fit()
            .centerCrop()
            .into(image_product_dialogue);
    textView_product_dialogue.setText(items.getName());

    builder.setView(view);

    builder.setNegativeButton("ADD TO CART", new 
    DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            // showConfirmDialogue(position, text_count.getNumber(), 
   text_count.getNumber());
            dialog.dismiss();

        }
    });


    builder.show();

  }

 }

And call create your adapter class object like below -

RecyclerAdapter adapter = new RecyclerAdapter(options, YourActivity.this); // If you are creating adapter class object from fragment then replace `YourActivity.this` with `getActivity()`.
recyclerView.setAdapter(adapter);
Al-Amin
  • 1,369
  • 1
  • 9
  • 26