Reputation: 13
I'm trying to connect my app to a Firebase Database, but whenever I launch the code that problem show up
that's my main activity
public class MainActivity extends AppCompatActivity {
private static final String TAG = "ViewDatabase";
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference myRef;
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFirebaseDatabase = FirebaseDatabase.getInstance();
myRef = mFirebaseDatabase.getReference("Discount/Business");
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
UserInformation uInfo = new UserInformation();
uInfo.setAltro(ds.child("Altro").getValue(UserInformation.class).getAltro());
uInfo.setCognome(ds.child("Cognome").getValue(UserInformation.class).getCognome());
uInfo.setInformazioni(ds.child("Informazioni").getValue(UserInformation.class).getInformazioni());
uInfo.setNome(ds.child("Nome").getValue(UserInformation.class).getNome());
Log.d(TAG, "showData: Altro: " + uInfo.getAltro());
Log.d(TAG, "showData: Cognome: " + uInfo.getCognome());
Log.d(TAG, "showData: Informazioni: " + uInfo.getInformazioni());
Log.d(TAG, "showData: Nome: " + uInfo.getNome());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
that's my User class
public class UserInformation {
private String Altro;
private String Cognome;
private String Informazioni;
private String Nome;
public UserInformation(){
}
public String getAltro() {
return Altro;
}
public void setAltro(String altro) {
Altro = altro;
}
public String getCognome() {
return Cognome;
}
public void setCognome(String cognome) {
Cognome = cognome;
}
public String getInformazioni() {
return Informazioni;
}
public void setInformazioni(String informazioni) {
Informazioni = informazioni;
}
public String getNome() {
return Nome;
}
public void setNome(String nome) {
Nome = nome;
}
and my log error
11-14 10:59:55.016 2909-2909/com.app8 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.app8, PID: 2909
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Long to type com.app8.UserInformation
at com.google.android.gms.internal.zzbqi.zze(Unknown Source)
at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
at com.google.android.gms.internal.zzbqi.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.app8.MainActivity$1.onDataChange(MainActivity.java:41)
at com.google.android.gms.internal.zzbmz.zza(Unknown Source)
at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source)
at com.google.android.gms.internal.zzboc$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
It's my first time using Firebase, I've seen other questions related to the topic but I can't figure out how to make it works, I hope you can help me.
thanks for your help, any suggestion is highly appreciated
Upvotes: 1
Views: 3414
Reputation: 2718
I figured out the actual reason of this error. This issue occurred to me also just now. What I came to know was there was some sort of garbage data collection occurred in the firebase database.
Solution : Open Firebase console -> Go to Database -> Delete all the database data -> Uninstall app from phone -> Reinstall -> Error Solved !!
Happy Coding!
Upvotes: 1
Reputation: 138979
To get those values out from the database, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference businessRef = rootRef.child("Discount").child("Business");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String altro = ds.child("Altro").getValue(String.class);
String cognome = ds.child("Cognome").getValue(String.class);
String informazioni = ds.child("Informazioni").getValue(String.class);
String nome = ds.child("Nome").getValue(String.class);
Log.d("TAG", altro + " / " + cognome + " / " + informazioni + " / " + nome);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
businessRef.addListenerForSingleValueEvent(eventListener);
As an advice, use in the future all fields according cu Java Naming Conventions.
Upvotes: 1
Reputation: 1
Can you try below.
UserInformation uiFromSnapShot = ds.getValue(UserInformation.class); uInfo.setXXX(uiFromSnapShot.getXXX);
Upvotes: 0