【问题标题】:Could not deserialize object. Failed to convert a value of type java.lang.String to long无法反序列化对象。无法将 java.lang.String 类型的值转换为 long
【发布时间】:2019-05-10 17:14:28
【问题描述】:

我想从 firebase 中提取数据我有两个 digit 类型的字段,这会导致兼容性问题

除数字以外的其他字符数据都有效

package fr.halas.loginhalas.Filter;

import com.google.firebase.firestore.IgnoreExtraProperties;

@IgnoreExtraProperties
public class Affichage {


    public static final String FIELD_CREATOR_NAME = "CreatorName";
    public static final String FIELD_USERID = "UserID";
    public static final String FIELD_MODULE = "Module";
    public static final String FIELD_SECTION = "Section";
    public static final String FIELD_GROUPE = "Groupe";


    private String CreatorName;
    private String Module;
    private String UserID;
    private long Section;
    private long Groupe;



    public Affichage(String CreatorName,String UserID,String Module, long Section, long Groupe) {
        this.CreatorName = CreatorName;
        this.UserID = UserID;
        this.Module = Module;
        this.Section = Section;
        this.Groupe = Groupe;
    }

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

    public String getCreatorName() {
        return CreatorName;
    }

    public void setCreatorName(String creatorName) {
        CreatorName = creatorName;
    }

    public String getModule() {
        return Module;
    }

    public void setModule(String module) {
        Module = module;
    }

    public String getUserID() {
        return UserID;
    }

    public void setUserID(String userID) {
        UserID = userID;
    }

    public long getSection() {
        return Section;
    }

    public void setSection(long section) {
        Section = section;
    }

    public long getGroupe() {
        return Groupe;
    }

    public void setGroupe(long groupe) {
        Groupe = groupe;
    }


}

  @SuppressLint("SetTextI18n")
        public void bind(final DocumentSnapshot snapshot, final OnAffichageSelectedListener listener) {

            final Affichage affichage = snapshot.toObject(Affichage.class); // Error is here ! 

            creator.setText(affichage.getCreatorName());
            moduleView.setText(affichage.getModule());
            groupeView.setText("Groupe: " + affichage.getGroupe());
            sectionView.setText("Section: " + affichage.getSection());

错误信息是 05-10 18:00:36.850 19488-19488/fr.halas.loginhalas E/AndroidRuntime: 致命异常: main 进程:fr.halas.loginhalas,PID:19488 java.lang.RuntimeException:无法反序列化对象。无法将 java.lang.String 类型的值转换为 long(在字段 'Groupe' 中找到)

【问题讨论】:

  • @AKSW (Groupe & Section) 的类型是数字

标签: java android firebase google-cloud-firestore


【解决方案1】:

在我的手机上完全删除应用程序并重新启动编译后正常启动而没有错误,应用程序必须提供无效缓存

【讨论】:

    【解决方案2】:

    这两行:

        groupeView.setText("Groupe: " + affichage.getGroupe());
        sectionView.setText("Section: " + affichage.getSection());
    

    应该是:

        groupeView.setText("Groupe: " + Long.toString(affichage.getGroupe());
        sectionView.setText("Section: " + Long.toString(affichage.getSection());
    

    【讨论】:

    • 为什么这在 Java 中是必要的?
    • .setText 正在寻找字符串值,affichage.getGroupe() 和 getSection() 返回 long 值,需要将其转换为字符串才能使 setText 工作
    • 好吧,Java 会自动执行此操作,这不是 Python 或类似的。左边是一个字符串,右边是一个原始数字,它将被自动装箱为对象类型Long,然后自动调用toString()。同样,这与 Python 或其他语言不同
    • 是的,这是错误的,错误是从字符串到长的错误解析,没有长到字符串,是的,java 自动执行此操作,代码甚至无法编译,并且错误发生在几行以上
    猜你喜欢
    • 2021-09-30
    • 2019-12-09
    • 2018-09-29
    • 2018-09-18
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多