Talk about beauty? So, I tried to create a custom Aler Dialog, but every time I try to save it, it gives a NullPointerException error, but I do not know what the cause is. The code for the method that Alert has is this:
Button btnAlertDataConsulta;
Button btnAlertHoraConsulta;
EditText edtNomeDrConsulta;
@OnClick(R.id.consultas_btn_agendarconsulta)
void agendarConsulta(){
final LayoutInflater inflater = LayoutInflater.from(getActivity());
consultas = new ArrayList<>();
AlertDialog.Builder builder = new AlertDialog.Builder(this.getContext());
builder.setView(inflater.inflate(R.layout.alert_consultas, null));
final AlertDialog alert = builder.create();
builder.setTitle("Nova Consulta");
builder.setPositiveButton("Salvar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
btnAlertDataConsulta = alert.findViewById(R.id.btn_alert_data_consulta);
btnAlertHoraConsulta = alert.findViewById(R.id.btn_alert_hora_consulta);
edtNomeDrConsulta = alert.findViewById(R.id.edt_alert_nome_dr_consulta);
btnAlertDataConsulta.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("LOG", "ALERT DATA CONSULTA ");
}
});
btnAlertHoraConsulta.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("LOG", "ALERT HORA CONSULTA ");
}
});
consultas.add(new Consulta("Dra. Jane Doe", "28/08", "15h"));
bDAO.inserirConsultas(new Consulta("Dra. Jane Doe", "28/08", "15h"));
loadRecycler(consultas);
Helper.snackbarFast(getView(), "Consulta adicionada!");
Log.i("LOG", "Data: 21/01 | Nome Dr: "+edtNomeDrConsulta.getText().toString()+" | Hora: 21h");
}
});
builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.show();
}
and it returns me this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener (android.view.View $ OnClickListener) ' on a null object reference at project.test.project.app.view.ConsultsFragment $ 2.onClick (QueriesFragment.java:134) at android.support.v7.app.AlertController $ ButtonHandler.handleMessage (AlertController.java:162) at android.os.Handler.dispatchMessage (Handler.java:102) at android.os.Looper.loop (Looper.java:154) at android.app.ActivityThread.main (ActivityThread.java:6123) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:867) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:757)