AlertDialog Custom Layout - NullPointerException error

0

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)

    
asked by anonymous 29.12.2017 / 20:34

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 in the layout that inflated, you have to search the view, in case you were looking for Alert, so the NullPointer. Just one detail, the onclick of the btnAlertDataConsulta and btnAlertHorseQuery buttons will not execute. You need to declare all that out of the positive button onclick, findview and onclick.

    
08.01.2018 / 17:19
0

I think I have a short version that can 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();
    
29.12.2017 / 21:55