Firebase数据库getValue问题

时间:2017-04-16 12:00:22

标签: android exception firebase firebase-realtime-database getvalue

我的应用提供了以下错误: 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代码: enter image description here

如果有人可以帮助我找到我的错误,谢谢。

编辑:游戏类:

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 = "";
}

最重要的是,我有另一个类,其中包含User类的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)呢? 谢谢!

1 个答案:

答案 0 :(得分:0)

感谢您的帮助,但经过3天的努力,我找到了发生错误的行。我还是不知道为什么。

在游戏课中,我有一些getter和setter,当用不使用Integer.parsInt的代码替换clockHours的setter时,它工作....!

 public void setClockHours(String clockHours){

    this.clockHours = Integer.parseInt(clockHours);
}

非常感谢你的时间