0
You guys talking 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 don’t know what the cause is. The code of the method that has Alert 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 he returns me this mistake:
java.lang.Nullpointerexception: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$Onclicklistener)' on a null Object Reference at project.teste.com.br.projecto_app.view.Consultasfragment$2.onClick(Consultasfragment.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)
but this does not have a customized layout :(
– Wallace Baldenebre