1

I have added one button "btn". I have set onclicklistener, inside the button "btn" I have added another button "btnYes" to show display custom dialog when I add these "btnYes" apps get crash.

When I remove the "btnyes" button app is working. Can we add onclicklistener for two button inside one button for different work?

Java Code

public class MainActivity extends AppCompatActivity {
    private Button btn, btnYes, btnNo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = findViewById(R.id.click);
        btnYes = findViewById(R.id.yes);
        btnNo = findViewById(R.id.no);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder dialogBox = new AlertDialog.Builder(MainActivity.this);
                LayoutInflater inflater = LayoutInflater.from(MainActivity.this);

                View myView = inflater.inflate(R.layout.custom_dialogbox, null);
                dialogBox.setView(myView);
                final AlertDialog mybuilder = dialogBox.create();
                mybuilder.setCancelable(false);
              btnYes.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      mybuilder.dismiss();

                  }
              });


            }
        });
    }
}

Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.example.customdialog.MainActivity$1.onClick(MainActivity.java:33)

Nageshwor Shah
  • 101
  • 1
  • 1
  • 8
  • Are your `btnYes` and `btnNo` buttons on your main activity or on the dialog? If they are on the dialog, you shouldn't initialize them using the `MainActivity`'s `findViewById` method as they do not exist there and thus the references will be `null`. Rather, call the `findViewById` on the `AlertDialog`'s `View` object, after inflating it inside the outer listener. – pappbence96 Sep 20 '19 at 08:46

2 Answers2

2

If btnYes and btnNo are on the dialog, then you should initialize these buttons using the AlertDialog's View object.

You have to modify your code like following.

public class MainActivity extends AppCompatActivity {
    private Button btn, btnYes, btnNo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = findViewById(R.id.click);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder dialogBox = new AlertDialog.Builder(MainActivity.this);
                LayoutInflater inflater = LayoutInflater.from(MainActivity.this);

                View myView = inflater.inflate(R.layout.custom_dialogbox, null);

                // these button should be initialize here.
                 btnYes = myView.findViewById(R.id.yes);
                 btnNo = myView.findViewById(R.id.no);

                dialogBox.setView(myView);
                final AlertDialog mybuilder = dialogBox.create();
                mybuilder.setCancelable(false);
              btnYes.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      mybuilder.dismiss();

                  }
              });


            }
        });
    }
}

Hope it helps you.

Jakir Hossain
  • 3,830
  • 1
  • 15
  • 29
1

if you want to display Dialog with choices yes or no, use this

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(message)
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        // action for yes

                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        // action for no

                    }
                });
        AlertDialog alert = builder.create();
        alert.show();

But, if you want to use a custom dialog layout, you should do it like this.

final Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.YOUR_LAYOUT_HERE);
        Button btnyes= dialog.findViewById(R.id.btnyes);

btnyes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
L2_Paver
  • 596
  • 5
  • 11