I'm trying to add records in the Firebase Realtime Database, through an AlertDialog, but the EditText components do not let me abstract the information from the components.
Below is the java excerpt ..
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
//Alerta para alterar informações do usuario.
Snackbar.make(view, "Adicionando um novo registro", Snackbar.LENGTH_LONG)
.setAction("Registro", null).show();
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = (MainActivity.this).getLayoutInflater();
builder.setTitle("Registro");
builder.setView(inflater.inflate(R.layout.fab_registro,null));
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
EditText data , nome , idade , horaExame , procedimento ,qtsPeliculas ;
nome = findViewById(R.id.registro_nome);
data = findViewById(R.id.registro_data);
horaExame = findViewById(R.id.registro_hora);
idade = findViewById(R.id.registro_idade);
qtsPeliculas = findViewById(R.id.registro_peliculas);
procedimento = findViewById(R.id.registro_procedimento);
database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Paciente");
String child = nome.getText().toString();
myRef.child(child).child("hora_exame").setValue("enrique");
myRef.child(child).child("procedimento").setValue("toráx");
myRef.child(child).child("peliculas").setValue("3");
myRef.child(child).child("data").setValue("23456");
myRef.child(child).child("idade").setValue("42");
myRef.child(child).child("nome").setValue("enriqeu");
}
});
builder.setNegativeButton("Cancelar", null);
builder.create();
builder.show();
}
});
This is the Error Logcat
Attempt to invoke virtual method 'void android.widget.EditText.getText(java.lang.CharSequence)' on a null object reference
at com.martel.unimedradiologia_controledeestoque.MainActivity$1$1.onClick(MainActivity.java:63)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:164)
The layout xml is fine, both the type and the id.