【发布时间】:2017-04-16 12:00:22
【问题描述】:
我的应用程序出现以下错误: 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());
// ...
}
});
如果有人可以帮助我找到我的错误,谢谢。
编辑:游戏类:
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();
}
编辑:用户类
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 = "";
}
除此之外,我还有另一个类,其中有一个用户类的 dataSnapshot.getValue。它工作得很好,这是怎么回事?代码如下:
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)? 谢谢!
【问题讨论】:
-
游戏模型类中的数据类型可能与您从dataSnapshot获取的值不同,请检查一次或提供您的模型类
-
Game类的数据类型是什么意思?我会把它添加到帖子中。
-
还有用户类?
-
顺便说一句,持续时间是一个字符串
-
是的,持续时间在 Game 类的方法中被转换为字符串。我将添加 User 类。顺便说一句,我也在“用户”上使用 datasnapshot.getValue 但那里没有问题....
标签: android exception firebase firebase-realtime-database getvalue