我目前正在使用firebase来存储我的所有信息,我使用firebase身份验证进行了用户注册,并将该用户扩展到重新使用身份验证中创建的id的数据库,现在我需要让所有用户将他的手机与如果数字匹配,那么手机会与用户一起插入并检索该特定用户,所以我这样做了:
代码
FirebaseDatabase.getInstance().getReference().child("Users")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Log.d("snapshot", String.valueOf(snapshot));
Log.d("snap",String.valueOf(snapshot.getValue(User.class)));
User user = snapshot.getValue(User.class);
if(number.equals(String.valueOf(user.getPhone()))){
userFriend = user;
}
}
if(userFriend == null){
//to do snackbar
}
}
用户类
public class User {
private String id;
private String username;
private int age = -1;
private String phone;
private int gamesPlayed = 0;
private ArrayList<User> friends = new ArrayList<User>();
// CONSTRUCTOR
public User() {
}
public User(String username) {
this.username = username;
}
// GETTERS & SETTERS
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public User(String id, String username, int age, String phone, int gamesPlayed, ArrayList<User> users) {
this.id = id;
this.username = username;
this.age = age;
this.phone = phone;
this.gamesPlayed = gamesPlayed;
this.friends = users;
}
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
我的数据库结构:
以下是我在堆栈跟踪中的内容:
04-20 16:06:04.070 30972-30972/com.esmad.pdm.friendlymanager E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.esmad.pdm.friendlymanager, PID: 30972
com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String
at com.google.android.gms.internal.zzbtg.zzaF(Unknown Source)
at com.google.android.gms.internal.zzbtg.zzb(Unknown Source)
at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
at com.google.android.gms.internal.zzbtg.zzb(Unknown Source)
at com.google.android.gms.internal.zzbtg$zza.zze(Unknown Source)
at com.google.android.gms.internal.zzbtg$zza.zzaH(Unknown Source)
at com.google.android.gms.internal.zzbtg.zze(Unknown Source)
at com.google.android.gms.internal.zzbtg.zzb(Unknown Source)
at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.esmad.pdm.friendlymanager.Friends$1.onDataChange(Friends.java:61)
at com.google.firebase.database.Query$1.onDataChange(Unknown Source)
at com.google.android.gms.internal.zzbpx.zza(Unknown Source)
at com.google.android.gms.internal.zzbqx.zzZS(Unknown Source)
at com.google.android.gms.internal.zzbra$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)
错误与此行有关:User user = snapshot.getValue(User.class);
我怀疑它与我构建User模型的方式和我检索的数据有关,有些东西不兼容。我在我的模型gamesPlayed和年龄有2个int,但是当我将用户传递给快照时我不知道如何获取2个特定值,所以我可以传递它们,我是否需要重新设计用户实例?或者一切都很好,请帮助我混淆:/`
答案 0 :(得分:0)
在您的用户类中,您的手机字段必须是长类型。