0

我的应用程序出现以下错误: 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 代码: 在此处输入图像描述

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

编辑:游戏类:

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)?谢谢!

4

1 回答 1

0

感谢您的帮助,但经过 3 天的努力,我找到了导致错误的行。我仍然不知道为什么。

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

 public void setClockHours(String clockHours){

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

无论如何谢谢你的时间

于 2017-04-25T18:15:32.383 回答