I am having an error when I perform queries in firebase, my code for query looks like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
You're most likely getting an error because cpf
is null when you use it as a child in mReferenceplaca
. This happens because Firebase downloads data asynchronously and your code lines are executed synchronously. Therefore, by the time this line mReferencePlaca.child("Funcionario").child(cpf).child("placa")
is executed, the value of cpf
is still null because this cpf = dataSnapshot.getValue(String.class);
hasn't happened yet.
To solve this, change this :-
DatabaseReference mReferenceCpf = FirebaseDatabase.getInstance().getReference();
databaseReference.child("Usuario").child(uId).child("cnpjCpf")
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
cpf = dataSnapshot.getValue(String.class);
txtCpf.setText(cpf);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
DatabaseReference mReferencePlaca = FirebaseDatabase.getInstance().getReference();
mReferencePlaca.child("Funcionario").child(cpf).child("placa")
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
placa = dataSnapshot.getValue(String.class);
txtPlaca.setText(placa);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
to this :-
DatabaseReference mReferenceCpf = FirebaseDatabase.getInstance().getReference();
databaseReference.child("Usuario").child(uId).child("cnpjCpf")
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
cpf = dataSnapshot.getValue(String.class);
txtCpf.setText(cps);
DatabaseReference mReferencePlaca = FirebaseDatabase.getInstance().getReference();
mReferencePlaca.child("Funcionario").child(cpf).child("placa")
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
placa = dataSnapshot.getValue(String.class);
txtPlaca.setText(placa);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
not sure if this solution will be helpful for others.
background : I was setting firebase path value with intent parcel.
Solution : resolved this issue by updating androidmenifest as adding 'singleInstance'
<activity android:name=".yourActivity"
android:launchMode="singleInstance"
android:parentActivityName=".MainActivity">
To solve your problem, just declare nome
, cpf
and placa
Strings inside the coresponding onDataChange()
methods, otherwise is null, due the asynchronous behaviour.
As an example, please use this code:
public void onDataChange(DataSnapshot dataSnapshot) {
String nome = dataSnapshot.getValue(String.class);
TextView txtNome = (TextView) findViewById(R.id.txtNomePerfil);
txtNome.setText(nome);
}
In the same way, you need to make the other changes.
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
In the onCreate() method, initialize the FirebaseAuth instance and the AuthStateListener method so you can track whenever the user signs in or out.
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
// ...
}
};
Attach the listener to your FirebaseAuth instance in the onStart() method and remove it on onStop().
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
https://firebase.google.com/docs/auth/android/start/?hl=es-419