I am trying to develop a screen consisting of buttons where clicking a button launches a custom dialog. The dialog has a ok button that closes it. I wrote the following code for it but is giving me a NullPointerException :
Dialog modalDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.input);
Button modal = (Button)findViewById(R.id.ModalityButton);
modal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
modalDialog = new Dialog(Input.this);
modalDialog.setContentView(R.layout.modality);
modalDialog.setCancelable(true);
Button ok = (Button) findViewById(R.id.modality_ok);
ok.setOnClickListener(new OnClickListener() { // shows null pointer error at this line
@Override
public void onClick(View v) {
modalDialog.cancel();
}
});
modalDialog.show();
}
});
}
Search for the button
okin the dialog’s layout: