0
I am trying to add records in the Firebase Realtime Database, through a Alertdialog, but the Edittext components do not let me abstract the component information.
Below is the section of java..
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 Logcat of error
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)
xml layout is all right, both type and id.
Yes, this chunk is inside onCreate. even creating at the beginning of onCreate (outside the button) gives this problem. and regarding casting is not required to use, but tried with it tbm and was not.
– Enrique Martel
Are these views from the layout being inflated? This layout being inflated is the same as used in Mainacitivity?
– Natan Felipe
no, I created an XML layout only pro Fab in the case
– Enrique Martel
It would look like you’re posting the Mainactivity layout?
– Natan Felipe
Not right now, but. I’ll post as soon as I can, I can say it’s just a Listview with Fab in the corner.
– Enrique Martel
Quiet. Humm, so this edittext is in another layout that is not invoked by Mainactivity? Or is it in the Adapter layout in Listview?
– Natan Felipe
it is inflated in the onClick of Fab
– Enrique Martel
Yes, the onclick of Fab inflates a Alertdialog with a custom layout, there in the setPositiveButton method I try to string the values of the components and send to firebase.
– Enrique Martel