出现错误:无法反序列化对象。无法将 java.lang.String 类型的值转换为 long

Posted

技术标签:

【中文标题】出现错误:无法反序列化对象。无法将 java.lang.String 类型的值转换为 long【英文标题】:Getting error : Could not deserialize object. Failed to convert a value of type java.lang.String to long 【发布时间】:2021-09-30 17:02:39 【问题描述】:

enter image description here我已经尝试过多次更改值。现在我在 Firebas 中有相同的值。如果我在代码中手动提供值,它会起作用,但是当我尝试从 Firestore 获取信息时它不起作用。但是,我仍然不断收到此错误:

E/androidRuntime: FATAL EXCEPTION: main
Process: com.luteraa.luteraaesports, PID: 8828
java.lang.RuntimeException: Could not deserialize object. Failed to convert a value of type java.lang.String to long (found in field 'matchNumber')
    at com.google.firebase.firestore.util.CustomClassMapper.deserializeError(CustomClassMapper.java:563)
    at com.google.firebase.firestore.util.CustomClassMapper.convertLong(CustomClassMapper.java:434)
    at com.google.firebase.firestore.util.CustomClassMapper.deserializeToPrimitive(CustomClassMapper.java:326)
    at com.google.firebase.firestore.util.CustomClassMapper.deserializeToClass(CustomClassMapper.java:226)
    at com.google.firebase.firestore.util.CustomClassMapper.deserializeToType(CustomClassMapper.java:189)
    at com.google.firebase.firestore.util.CustomClassMapper.access$300(CustomClassMapper.java:54)
    at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:770)
    at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:741)
    at com.google.firebase.firestore.util.CustomClassMapper.convertBean(CustomClassMapper.java:542)
    at com.google.firebase.firestore.util.CustomClassMapper.deserializeToClass(CustomClassMapper.java:253)
    at com.google.firebase.firestore.util.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:100)
    at com.google.firebase.firestore.DocumentSnapshot.toObject(DocumentSnapshot.java:183)
    at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(QueryDocumentSnapshot.java:116)
    at com.google.firebase.firestore.DocumentSnapshot.toObject(DocumentSnapshot.java:161)
    at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(QueryDocumentSnapshot.java:97)
    at com.luteraa.luteraaesports.BgmiActivity$1.onEvent(BgmiActivity.java:39)
    at com.luteraa.luteraaesports.BgmiActivity$1.onEvent(BgmiActivity.java:35)
    at com.google.firebase.firestore.Query.lambda$addSnapshotListenerInternal$2$Query(Query.java:1133)
    at com.google.firebase.firestore.-$$Lambda$Query$JWhMgzcsIac1Z-exZj1pTDRisJg.onEvent(Unknown Source:6)
    at com.google.firebase.firestore.core.AsyncEventListener.lambda$onEvent$0$AsyncEventListener(AsyncEventListener.java:42)
    at com.google.firebase.firestore.core.-$$Lambda$AsyncEventListener$DNkggu2LY54oguDvcp-QtRg6Sfg.run(Unknown Source:6)
    at android.os.Handler.handleCallback(Handler.java:914)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:224)
    at android.app.ActivityThread.main(ActivityThread.java:7551)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995)

这是我模型的代码

public class BGMICategoryModel 
private String matchId;
private long matchNumber;
private String gameMode;

public BGMICategoryModel(String matchId, long matchNumber, String gameMode) 
    this.matchId = matchId;
    this.matchNumber = matchNumber;
    this.gameMode = gameMode;

public  BGMICategoryModel()

public String getMatchId() 
    return matchId;


public void setMatchId(String matchId) 
    this.matchId = matchId;


public long getMatchNumber() 
    return matchNumber;


public void setMatchNumber(long matchNumber) 
    this.matchNumber = matchNumber;


public String getGameMode() 
    return gameMode;


public void setGameMode(String gameMode) 
    this.gameMode = gameMode;

我的适配器代码

public class BGMICategoryAdapter extends RecyclerView.Adapter<BGMICategoryAdapter.BGMICategoryViewHolder> 

Context context;
ArrayList<BGMICategoryModel> bgmiCategoryModels;


public BGMICategoryAdapter(Context context, ArrayList<BGMICategoryModel> bgmiCategoryModels)

    this.context = context;
    this.bgmiCategoryModels = bgmiCategoryModels;


@NonNull
@NotNull
@Override
public BGMICategoryViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) 
    View view = LayoutInflater.from(context).inflate(R.layout.matches_bgmi,null);
    return new BGMICategoryViewHolder(view);



@Override
public void onBindViewHolder(@NonNull @NotNull BGMICategoryAdapter.BGMICategoryViewHolder holder, int position) 

    BGMICategoryModel model = bgmiCategoryModels.get(position);

    holder.matchNumber.setText(String.valueOf(model.getMatchNumber()));
    holder.gameMode.setText(model.getGameMode());


@Override
public int getItemCount() 
    return bgmiCategoryModels.size();


public class BGMICategoryViewHolder extends RecyclerView.ViewHolder
    TextView matchNumber, gameMode;

    public BGMICategoryViewHolder(@NonNull @NotNull View itemView) 
        super(itemView);
        matchNumber = itemView.findViewById(R.id.matchNumber);
        gameMode = itemView.findViewById(R.id.gameMode);
    

主要活动

public class BgmiActivity extends AppCompatActivity 
ActivityBgmiBinding activityBgmiBinding;
FirebaseFirestore fStore;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    activityBgmiBinding = ActivityBgmiBinding.inflate(getLayoutInflater());
    setContentView(activityBgmiBinding.getRoot());

    fStore = FirebaseFirestore.getInstance();

    ArrayList<BGMICategoryModel> bgmiCategoryModels = new ArrayList<>();
    //bgmiCategoryModels.add(new BGMICategoryModel("match1", 1, "PCM"));
    BGMICategoryAdapter adapter = new BGMICategoryAdapter(this, bgmiCategoryModels);

    fStore.collection("bgmiMatches").addSnapshotListener(new EventListener<QuerySnapshot>() 
        @Override
        public void onEvent(@Nullable @org.jetbrains.annotations.Nullable QuerySnapshot value, @Nullable @org.jetbrains.annotations.Nullable FirebaseFirestoreException error) 
            for (DocumentSnapshot snapshot : value.getDocuments())
                BGMICategoryModel model = snapshot.toObject(BGMICategoryModel.class);
                model.setMatchId(snapshot.getId());
                bgmiCategoryModels.add(model);
            
            adapter.notifyDataSetChanged();
        
    );
    activityBgmiBinding.bgmiContent.setLayoutManager(new LinearLayoutManager(this));
    activityBgmiBinding.bgmiContent.setAdapter(adapter);

我找不到解决方案。请帮我解决这个问题。谢谢。

【问题讨论】:

【参考方案1】:

您收到以下错误:

java.lang.RuntimeException:无法反序列化对象。无法将 java.lang.String 类型的值转换为 long(在字段“matchNumber”中找到)

因为您的matchNumber 字段在您的类中定义为long,而在数据库中保存一个字符串值。要摆脱这个异常,请将字段的类型改为数字,不是字符串,例如:

matchNumber: 12 //Correct

matchNumber: "12" //Incorrect. See the quotation marks?

【讨论】:

只有先生是这样的。在数据库中也是数字格式。但我仍然收到此错误。在数据库中,字段为 gameMode : "Battle Royale" 和 gameNumber : 54 您确定bgmiMatches 集合下的所有文档都有matchNumber 字段作为数字吗?你能检查一下吗?打印屏幕也可能有用。 是的先生,我已经在顶部添加了图像 您在哪一行代码中遇到了错误? 先生,错误指向这两行,在 com.luteraa.luteraaesports.BgmiActivity$1.onEvent(BgmiActivity.java:39) 在 com.luteraa.luteraaesports.BgmiActivity$1.onEvent(BgmiActivity .java:35)

以上是关于出现错误:无法反序列化对象。无法将 java.lang.String 类型的值转换为 long的主要内容,如果未能解决你的问题,请参考以下文章

无法反序列化当前 JSON 对象,xamarin.forms 中的错误

在 C# 中反序列化复杂对象

反序列化 Json 出现错误“无法反序列化当前 JSON 数组”

保存对象列表返回无法反序列化错误

Json.JsonSerializationException : 无法反序列化当前 JSON 对象

错误:预期状态。无法反序列化 JSON 对象