问题
I have big problem. Runing the app using Android Emulator works good, but when I put the app on a real phone i get this error :
java.lang.RuntimeException: Could not deserialize object. Failed to convert value of type java.lang.Long to boolean (found in field 'imp')
My Firebase data is OK, 'imp' field is a boolean in the database, so what I'm retrieving is a boolean from databae and storing it in a boolean var. How can it say that I receive a Long when the data is sent from Firebase as a Boolean?
Why is this happening only with a real phone?
Now I really don't understand why in Android Emulator works fine and installing the app in a real phone gives me this error.
This is my adapptor who is handling the data:
public class modelNoutati {
String name;
String cont;
Date data;
Boolean imp;
public modelNoutati(){ }
public modelNoutati(String cont, Date data, Boolean imp, String name) {
this.name = name;
this.cont = cont;
this.data = data;
this.imp = imp;
}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public Date getData() {return data;}
public void setData(Date data) {this.data = data;}
public String getCont() {return cont;}
public void setCont (String cont) {this.cont = cont; }
public Boolean getImp () {return imp;}
public void setImp (Boolean imp) {this.imp = imp;}
I get the issue here on doc.getDocument().toObject(modelNoutati.class)
for (DocumentChange doc : documentSnapshots.getDocumentChanges()){
if(doc.getType() == DocumentChange.Type.ADDED) {
modelNoutati newsInt = doc.getDocument().toObject(modelNoutati.class);
newsL.add(newsInt);
newsAdaptor.notifyDataSetChanged();
Logcat:
Process: projects.nv.umcstudent, PID: 14354
java.lang.RuntimeException: Could not deserialize object. Failed to convert value of type java.lang.Long to boolean (found in field 'imp')
at com.google.android.gms.internal.zzeym.zzc(Unknown Source)
at com.google.android.gms.internal.zzeym.zza(Unknown Source)
at com.google.android.gms.internal.zzeym.zzb(Unknown Source)
at com.google.android.gms.internal.zzeym.zza(Unknown Source)
at com.google.android.gms.internal.zzeym$zza.zza(Unknown Source)
at com.google.android.gms.internal.zzeym.zza(Unknown Source)
at com.google.android.gms.internal.zzeym.zza(Unknown Source)
at com.google.firebase.firestore.DocumentSnapshot.toObject(Unknown Source)
at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(Unknown Source)
at projects.nv.umcstudent.News$1.onEvent(News.java:70)
at projects.nv.umcstudent.News$1.onEvent(News.java:57)
at com.google.firebase.firestore.zzi.onEvent(Unknown Source)
at com.google.android.gms.internal.zzeyn.zza(Unknown Source)
at com.google.android.gms.internal.zzeyo.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:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
回答1:
The following line of code:
java.lang.RuntimeException: Could not deserialize object. Failed to convert value of type java.lang.Long to boolean (found in field 'imp')
Tells you exactly what the problem is. Your imp property is declared in your model class as a Boolean but when you are trying to get the value of this property from the database you are getting a Long. There is no way in Java in which you can convert the Long to a Boolean.
To solve this, you need to clear your database and add fresh data in which the imp property will hold a Boolean value as in your model class, and not a Long value.
Bafta! ;)
回答2:
This is not a big issue you are only trying to get the wrong casting type.
Go to your database and check imp property in all the records. Certainly you have mistakenly entered the wrong data type for this.
It should be boolean type but in one of your record it is wrongly entered as long.
you should also add try catch block around this mapping.
try{
modelNoutati newsInt = doc.getDocument().toObject(modelNoutati.class);
newsL.add(newsInt);
newsAdaptor.notifyDataSetChanged();
}catch(Exception e){
e.printStackTrace();
}
回答3:
Declare imp as int in your class modelNoutati and declare it as a String in your class database handler. Your imp equals 0(false) or 1(true).
I hope that help you :)
来源:https://stackoverflow.com/questions/49614685/java-lang-runtimeexception-could-not-deserialize-object-failed-to-convert-valu