Firebase DatabaseException:无法将 java.lang.Long 类型的值转换为 String

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39552348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:14:25  来源:igfitidea点击:

Firebase DatabaseException: Failed to convert value of type java.lang.Long to String

javaandroidfirebasefirebase-realtime-database

提问by Amy Spoiler

com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String

com.google.firebase.database.DatabaseException:无法将 java.lang.Long 类型的值转换为字符串

is the error I keep getting when following the docs in attempting to retrieve data to an object for use.

是我在遵循文档尝试将数据检索到对象以供使用时不断收到的错误。

Here is my object model

这是我的对象模型

public class User {

    private String tour_director_key;
    private String last_name;
    private String location_latitude;
    private String tour_id;
    private String photo;
    private String tour_director_name;
    private String middle_name;
    private String location_longitude;
    private String passenger_id;
    private long location_updated;
    private String tour_director;
    private String email;
    private String first_name;
    private String mobile_phone;
    private String td_id;

    public User() {
        // empty default constructor, necessary for Firebase to be able to deserialize users
    }

    public String getTour_director_key() {
        return tour_director_key;
    }
    public String getLast_name() {
        return last_name;
    }
    public String getLocation_latitude() {
        return location_latitude;
    }
    public String getTour_id() {
        return tour_id;
    }
    public String getPhoto() {
        return photo;
    }
    public String getTour_director_name() {
        return tour_director_name;
    }
    public String getMiddle_name() {
        return middle_name;
    }
    public String getLocation_longitude() {
        return location_longitude;
    }
    public String getPassenger_id() { return passenger_id; }
    public String getMobile_phone() { return mobile_phone; }
    public long getLocation_updated() {
        return location_updated;
    }
    public String getTour_director() {
        return tour_director;
    }
    public String getEmail() {
        return email;
    }
    public String getFirst_name() {
        return first_name;
    }
    public String getTd_id() { return td_id; }

}

Data on firebase for user:

用户的 firebase 数据:

IMG HEREand finally the code & line I get the error on is commented.

我在这里最后我得到错误的代码和行被注释了。

  Query userDataQuery = Constants.USER_REF.orderByKey().equalTo(mUserId);

  userDataQuery.addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
         for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
             //// ERROR COMES FROM THE LINE BELOW ////
             User currentUser = postSnapshot.getValue(User.class);
             Log.i("THE_CURRENT_USER:::", currentUser.toString());
             Log.i("THE_USERS_EMAIL:::", currentUser.getEmail());

             ...

Full Stacktrace:

完整的堆栈跟踪:

FATAL EXCEPTION: main
Process: app.timto.co.app, PID: 7453
com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String
    at com.google.android.gms.internal.zzaln.zzcc(Unknown Source)
    at com.google.android.gms.internal.zzaln.zzb(Unknown Source)
    at com.google.android.gms.internal.zzaln.zza(Unknown Source)
    at com.google.android.gms.internal.zzaln.zzb(Unknown Source)
    at com.google.android.gms.internal.zzaln$zza.zze(Unknown Source)
    at com.google.android.gms.internal.zzaln$zza.zzcc(Unknown Source)
    at com.google.android.gms.internal.zzaln.zzd(Unknown Source)
    at com.google.android.gms.internal.zzaln.zzb(Unknown Source)
    at com.google.android.gms.internal.zzaln.zza(Unknown Source)
    at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
    at app.timto.co.app.AttendanceActivity.onDataChange(AttendanceActivity.java:112)
    at com.google.android.gms.internal.zzaie.zza(Unknown Source)
    at com.google.android.gms.internal.zzaje.zzcta(Unknown Source)
    at com.google.android.gms.internal.zzajh.run(Unknown Source)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

采纳答案by Iulian

The problem is that you are creating the property "mobile_phone" as a String and on Firebase it is a Long type.

问题是您将属性“mobile_phone”创建为字符串,而在 Firebase 上它是 Long 类型。

Change:

改变:

private String mobile_phone;

To:

到:

private Long mobile_phone;

回答by Diego Venancio

Check if your getters and class/model are equals in firebase database.

检查您的 getter 和类/模型在 firebase 数据库中是否相等。

When method .getValue()retrieve datas it compare if signatures are same.

当方法.getValue()检索数据时,它比较签名是否相同。

example:
In class we have a getLong
enter image description here

示例:
在课堂上我们有一个 getLong
在此处输入图片说明

for get value retrieve in firebase database enter image description here

用于在 firebase 数据库中检索值 在此处输入图片说明

回答by Malik Faizan

Problems

问题

  1. If you are adding values in firebase database manually, the values will be in long data type; for example:
  1. 如果您在 firebase 数据库中手动添加值,则这些值将采用长数据类型;例如:

enter image description here

在此处输入图片说明

  1. If you are adding values by commands, the values will be saved in the database as string.
  1. 如果您通过命令添加值,这些值将作为字符串保存在数据库中。

Solution:

解决方案:

When you are getting the values from database, save the values in default data type. Then, when you want to reuse the value change it into the string by using the toString() method.

当您从数据库中获取值时,以默认数据类型保存这些值。然后,当您想要重用该值时,请使用 toString() 方法将其更改为字符串。

回答by Tariqul

If you want to access data directly without create any object, You can change datatype with String to long.class

如果您想直接访问数据而不创建任何对象,您可以将 String 的数据类型更改为 long.class

`String reward = ds.child("reward").getValue(String.class);` 
      to
`long reward = ds.child("reward").getValue(long.class);`

or

或者

public String reward;
    to
public long reward;

回答by DragonFire

For me it was choosing setText(CharSequence text) in the adapter which was used to populate the recycler view. Please see attached image for clarityenter image description here

对我来说,它在用于填充回收器视图的适配器中选择了 setText(CharSequence text)。为清楚起见,请参阅附图在此处输入图片说明

回答by Andromo Tech

Edit allthe DB entries by adding double quotelike "phone" : "900000000"and it will solve the problem, so e.g.:

编辑所有数据库的条目中加入双引号一样"phone" : "900000000",它会解决这个问题,所以如:

"phone" : 900000000
"name" : "xxxx",
"password" : 111111

changed to:

变成:

"phone" : "900000000",
"name" : "xxxx",
"password" : "111111"