Alertdialog Custom Layout - Nullpointerexception Error

Asked

Viewed 306 times

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)

2 answers

1

    final LayoutInflater inflater = LayoutInflater.from(getActivity());
    consultas = new ArrayList<>();

    AlertDialog.Builder builder = new AlertDialog.Builder(this.getContext());

    //modificação
    View view = inflater.inflate(R.layout.alert_consultas, null);
    builder.setView(view);
    //fim modificação

    final AlertDialog alert = builder.create();
    builder.setTitle("Nova Consulta");

    builder.setPositiveButton("Salvar", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

            //modificação
            btnAlertDataConsulta = view.findViewById(R.id.btn_alert_data_consulta);
            btnAlertHoraConsulta = view.findViewById(R.id.btn_alert_hora_consulta);
            edtNomeDrConsulta = view.findViewById(R.id.edt_alert_nome_dr_consulta);
            //fim modificação

            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();
}

As you want to use buttons of the layout that inflated, you have to search from the view, in case you were looking for Alert, that’s why Nullpointer. Just one detail, the onclick of the btnAlertDataConsult and btnAlertHoraConsult buttons will not run. You need to declare all this outside the onclick of the Positive button, findview and onclick.

0

I think I have a short version that might help you:

new AlertDialog.Builder(context)
.setTitle("Salvar ?").
setMessage("Deseja Salvar ?")
.setPositiveButton("Salvar",new DialogInterface.OnClickListener(){
    public void onClick(DialogInterface dialog,int whichButton){
        consultas.add(new Consulta("Dra. Jane Doe", "28/08", "15h"));
    }
})
.setNegativeButton("Cancelar",null).show();
  • but this does not have a customized layout :(

Browser other questions tagged

You are not signed in. Login or sign up in order to post.