gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:21:58 27 4
gpt4 key购买 nike

我正在使用 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);
}

跟踪运行类(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() 时,所有字段都以正确的数据类型存在。

最佳答案

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

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

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

更改参数类型或数据库值以保持一致。

关于java - Firebase 数据库异常无法将 long 转换为 String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45700522/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com