Firebase数据库异常无法将long转换为String

时间:2017-08-15 19:53:53

标签: java android firebase firebase-realtime-database

我正在使用Firebase实时数据库,我收到以下错误

FATAL EXCEPTION: main
                                                                    Process: com.example.tobias.run, PID: 17405
                                                                    com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String
                                                                        at com.google.android.gms.internal.zzbtg.zzaG(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.example.tobias.run.history.HistoryFragment$2.onChildAdded(HistoryFragment.java:123)
                                                                        at com.google.android.gms.internal.zzbox.zza(Unknown Source)
                                                                        at com.google.android.gms.internal.zzbqx.zzZV(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:241)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:6274)
                                                                        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)

尝试将从数据库中检索到的数据编组到类中时。这里发生错误

databaseRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            dataSnapshot.getValue(TrackedRun.class);
        }

跟踪运行类

public class TrackedRun implements Parcelable {

private float mDistance;
private long mTime;
private long mDate;
private int mRating;
private String mUnit;
private Integer mID = null;

public TrackedRun(int id, long date, float distance, long time, int rating, String unit){
    this.mDistance = distance;
    this.mTime = time;
    this.mDate = date;
    this.mRating = rating;
    this.mUnit = unit;
    this.mID = id;
}


public TrackedRun(){
    // Default constructor required for firebase calls to DataSnapshot.getValue(User.class)
}

public TrackedRun(Parcel in) {
    this.mID = in.readInt();
    this.mDate = in.readLong();
    this.mDistance = in.readFloat();
    this.mTime = in.readLong();
    this.mRating = in.readInt();
    this.mUnit = in.readString();
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeInt(mID);
    parcel.writeLong(mDate);
    parcel.writeFloat(mDistance);
    parcel.writeLong(mTime);
    parcel.writeInt(mRating);
    parcel.writeString(mUnit);
}

@Exclude
public static final Parcelable.Creator<TrackedRun> CREATOR = new Parcelable.Creator<TrackedRun>(){

    @Override
    public TrackedRun createFromParcel(Parcel parcel) {
        return new TrackedRun(parcel);
    }

    @Override
    public TrackedRun[] newArray(int i) {
        return new TrackedRun[i];
    }
};

public float getDistance(){
    return mDistance;
}
public long getTime(){
    return mTime;
}
public long getDate(){ return mDate; }
public int getRating(){
    return mRating;
}
public String getUnit(){
    return mUnit;
}
public Integer getId(){ return mID; }


public void setDistance(String distance){
    mDistance = DateManager.distanceToFloat(distance);
}
public void setTime(String time){
    mTime = DateManager.timeToUnix(time);
}
public void setDate(String date){
    mDate = DateManager.dateToUnix(date);
}
public void setRating(String rating){
    mRating = Integer.valueOf(rating);
}

public void setUnit(String unit){
    mUnit = unit;
}

Firebase database structure

我已经尝试将我所拥有的两个长字段更改为int,但是我得到了完全相同的错误。这与我正在实施Parcelable的事实有关吗?此外,当我运行dataSnapshot.getValue().toString()时,所有字段都以正确的数据类型存在。

2 个答案:

答案 0 :(得分:1)

setter方法的参数类型与数据库中的值不一致。例如:

public void setRating(String rating) {
    mRating = Integer.valueOf(rating);
}

这会导致Firebase在rating的数据库中期望一个字符串值。它找到Long值并报告它无法将Long转换为setRating()所需的字符串。 setDistance()setTime()setDate()存在同样的问题。

将参数类型或数据库值更改为一致。

答案 1 :(得分:0)

私有字符串mUnit;

您已将此变量定义为字符串,并在Firebase数据库中分配了一个整数值..在Firaebase数据库中将double涂层分配给了整数值

在Firebase数据库中

->

的理解

mUnit:1

制作->

mUnit:“ 1”