Firebase 数据库 getValue 问题
Firebase database getValue issue
我的应用出现以下错误:
com.google.firebase.database.DatabaseException: 无法将类型 java.lang.Long 的值转换为字符串
此错误发生在以下行:mGame = dataSnapshot.getValue(Game.class)
这是我的代码:
//create datarefence of only 1 game namely the current game
specificGameRef =mDatabase.child("games").child(-KhmBnF9vTLJSgNBcPqE);
specificGameRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mGame =dataSnapshot.getValue(Game.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
// ...
}
});
这是我来自 firebase 数据库的 json 代码:
如果有人能帮助我发现我的错误,谢谢。
编辑:游戏class:
public class Game {
//firebase
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference mDatabase;
FirebaseUser user;
User mUser;
private String creatorName;
private String key;
private int day;
private int month;
private int year;
private int clockHours;
private int clockMinutes;
private int durationMin;
private int durationMax;
private String creatorId;
public Game() //An empty constructor for datastorage of Firebase
{
}
public Game(String creatorId) {
this.creatorId = creatorId;
key = getKey();
day = getDay();
month = getMonth();
year = getYear();
clockHours = getClockHours();
clockMinutes = getClockMinutes();
durationMin = getDurationMin();
durationMax = getDurationMax();
}
编辑:用户class
public class User {
//firebase authentification system
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference mDatabase;
String userId;
String mUsername;
String email;
int age;
int stars;
int badReputation;
String gender;
int birthYear;
int birthMonth;
int birthDay;
public User()
{
}
public User(String userId,String mUsername,String email) {
mDatabase = FirebaseDatabase.getInstance().getReference();
this.userId = userId;
this.mUsername = mUsername;
age = getAge();
stars = 0;
this.email=email;
badReputation = 0;
gender = "";
}
除此之外,我还有另一个 class,其中 dataSnapshot.getValue 是用户 class。而且效果很好,这是怎么回事??这是代码:
public void retrieveUser()
{
//create datarefence of only 1 user namely the current user
DatabaseReference specificCreatorRef =mDatabase.child("users").child(user.getUid());
specificCreatorRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Now retrieving the current user and putting it in object mUser
mUser = dataSnapshot.getValue(User.class);
Log.w(TAGESS, "Print STARS of current mUSer: " + mUser.getStars());
creatorGender =mUser.getGender();
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
// ...
}
});
}
所以这最后一个代码工作完美,那么为什么这个工作而不是 DataSnapshot.getValue(Game.class)?
谢谢!
感谢您的帮助,但经过 3 天的努力,我找到了出错的行。我还是不知道为什么。
在游戏中 class 我有一些吸气剂和 setters,当用没有使用 Integer.parsInt 的代码替换 clockHours 的 setter 时,它起作用了....!
public void setClockHours(String clockHours){
this.clockHours = Integer.parseInt(clockHours);
}
谢谢你的时间
我的应用出现以下错误: com.google.firebase.database.DatabaseException: 无法将类型 java.lang.Long 的值转换为字符串
此错误发生在以下行:mGame = dataSnapshot.getValue(Game.class) 这是我的代码:
//create datarefence of only 1 game namely the current game
specificGameRef =mDatabase.child("games").child(-KhmBnF9vTLJSgNBcPqE);
specificGameRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mGame =dataSnapshot.getValue(Game.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
// ...
}
});
这是我来自 firebase 数据库的 json 代码:
如果有人能帮助我发现我的错误,谢谢。
编辑:游戏class:
public class Game {
//firebase
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference mDatabase;
FirebaseUser user;
User mUser;
private String creatorName;
private String key;
private int day;
private int month;
private int year;
private int clockHours;
private int clockMinutes;
private int durationMin;
private int durationMax;
private String creatorId;
public Game() //An empty constructor for datastorage of Firebase
{
}
public Game(String creatorId) {
this.creatorId = creatorId;
key = getKey();
day = getDay();
month = getMonth();
year = getYear();
clockHours = getClockHours();
clockMinutes = getClockMinutes();
durationMin = getDurationMin();
durationMax = getDurationMax();
}
编辑:用户class
public class User {
//firebase authentification system
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference mDatabase;
String userId;
String mUsername;
String email;
int age;
int stars;
int badReputation;
String gender;
int birthYear;
int birthMonth;
int birthDay;
public User()
{
}
public User(String userId,String mUsername,String email) {
mDatabase = FirebaseDatabase.getInstance().getReference();
this.userId = userId;
this.mUsername = mUsername;
age = getAge();
stars = 0;
this.email=email;
badReputation = 0;
gender = "";
}
除此之外,我还有另一个 class,其中 dataSnapshot.getValue 是用户 class。而且效果很好,这是怎么回事??这是代码:
public void retrieveUser()
{
//create datarefence of only 1 user namely the current user
DatabaseReference specificCreatorRef =mDatabase.child("users").child(user.getUid());
specificCreatorRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Now retrieving the current user and putting it in object mUser
mUser = dataSnapshot.getValue(User.class);
Log.w(TAGESS, "Print STARS of current mUSer: " + mUser.getStars());
creatorGender =mUser.getGender();
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
// ...
}
});
}
所以这最后一个代码工作完美,那么为什么这个工作而不是 DataSnapshot.getValue(Game.class)? 谢谢!
感谢您的帮助,但经过 3 天的努力,我找到了出错的行。我还是不知道为什么。
在游戏中 class 我有一些吸气剂和 setters,当用没有使用 Integer.parsInt 的代码替换 clockHours 的 setter 时,它起作用了....!
public void setClockHours(String clockHours){
this.clockHours = Integer.parseInt(clockHours);
}
谢谢你的时间