Firebase Android - Crash with “Found a conflicting setters with name: setGregorianChange” when .setValue()

痴心易碎 提交于 2020-12-09 06:30:08

问题


I'm playing with FirebaseDatabase and I can't resolve a very annoying crash.

Everytime I try to execute: FirebaseDatabase.getInstance().getReference().child("...").push().setValue(...)

The app crash and I have this message :

com.google.firebase.database.DatabaseException: Found a conflicting setters with name: setGregorianChange (conflicts with setGregorianChange defined on java.util.GregorianCalendar)
at com.google.android.gms.internal.zzbqi$zza.<init>(Unknown Source)
at com.google.android.gms.internal.zzbqi.zzi(Unknown Source)
at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
at com.google.android.gms.internal.zzbqi.zzaE(Unknown Source)
at com.google.android.gms.internal.zzbqi$zza.zzaF(Unknown Source)
at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
at com.google.android.gms.internal.zzbqi.zzaw(Unknown Source)
at com.google.firebase.database.DatabaseReference.zza(Unknown Source)
at com.google.firebase.database.DatabaseReference.setValue(Unknown Source)

This crash start happening after I tried to save an Object which contained a Calendar in FirebaseDatabase.

The problem is that, now, even if I remove the Calendar from this Object, the app keep crashing with the same error. It looks like something has been corrupted and I can't make it works again.

I have uninstall the app from my phone, clear the Database in Firebase Console but that didn't fix the problem.

Does one of you can help me?

----- EDIT -----

I did some new tests and actually the app keep crashing only if the class that I pass in .setValue(...) contains this method :

public Calendar getStartTimeCalendar() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(startTime);
    return calendar;
}

回答1:


I found the answer (with your help Deepesh).

The class I want to save in FirebaseDatabase contains these variables :

public long startTime;
public long endTime;

And the app crash when I have this method (probably because Firebase think that they are getters) :

public Calendar getStartTimeCalendar() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(startTime);
    return calendar;
}

public Calendar getEndTimeCalendar() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(endTime);
    return calendar;
}

The solution is to prefix those methods by @Exclude




回答2:


The Firebase Database is a JSON database, which means that it natively can only store JSON types. If you're trying to store any other type of information, it will have to converted between that type and an underlying JSON type, a process known as serializing/deserializing the data.

The Java-to-JSON serialization/deserialization that is built into the Firebase Database SDK for Android can serialize/deserialize all supported JSON types and can handle simple Java objects that consist of these types. It cannot (and isn't meant to) deal with most pre-existing classes in the Android or Java SDK, such as java.util.GregorianCalendar.

If you want to store a date in the Firebase Database, you'll have to come up with your own way of storing them. For dates this typically comes down to one of these ways:

  1. store the date as a UNIX timestamp (milliseconds since the epoch)
  2. store the date as separate year, month and day properties
  3. store the date as a string

How to implement these, depends a bit on how you got the GregorianCalendar in the first place.




回答3:


IgnoreExtraProperties annotation doesn't work even if you have no field that get could correspond with



来源:https://stackoverflow.com/questions/41917562/firebase-android-crash-with-found-a-conflicting-setters-with-name-setgregori

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!