0

i have recyclerview in my App and when list item is pressed bottomsheet dialog expends where i have two buttons but i can't set click listeners to it. This is onClick method in adapter

BottomSheetDialog offer_info_dialog;
RelativeLayout rel;
Button Yes,No;
View parentView;
@Override
public void onClick(View view) {
    rel = (RelativeLayout) activity.findViewById(R.id.bottomsheet);
    Yes = (Button) offer_info_dialog.findViewById(R.id.confirm_btn_on_info);
    No = (Button) offer_info_dialog.findViewById(R.id.cancel_btn_on_info);
        offer_info_dialog = new BottomSheetDialog(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        parentView = inflater.inflate(R.layout.offer_info_layout, null);
        offer_info_dialog.setContentView(parentView);
        ((View) parentView.getParent()).setBackgroundColor(context.getResources().getColor(android.R.color.transparent));
        offer_info_dialog.show();
        rel.setVisibility(View.INVISIBLE);

    Yes.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MainActivity.removeCheatOfferMarkers(2);
        }
    });

    No.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from((View) parentView.getParent());
            bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
        }
    });
    offer_info_dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            rel.setVisibility(View.VISIBLE);
        }
    });

}

and this is an error:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.design.widget.BottomSheetDialog.findViewById(int)' on a null object reference
Tornike Kurdadze
  • 955
  • 2
  • 10
  • 24
  • 1
    [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Ivan Pronin Jul 05 '17 at 20:31

1 Answers1

1

Multiple problems here.

  1. You are calling findViewById before you have inflated your view. This will cause nullpointer exceptions. findViewById should only be called AFTER inflater.inflate has been called, except in the case of your activity which I assume has already been inflated.

  2. Take a look at your code. offer_info_dialog has not been initialized to anything before you try to start calling methods on it, which is why you are getting a nullpointer.

astrade
  • 222
  • 1
  • 3
  • 10