我在 Cloud Firestore enter image description here
中添加了一些数据其中我有String title,String desc,String [] photos
我想在 MyClass.class 中获取该数据,因此我提供了参数化构造函数和 Getter&在课堂上安装。
但错误表示转换为阵列不支持,请改用列表
这是我的逻辑部分代码:
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("gallery")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d("MyTag", document.getId() + " => " + document.getData());
MyClass obj = document.toObject(MyClass.class);
}
} else {
Log.d("Main", "Error getting documents: ", task.getException());
}
}
});
这是我的班级:
public class MyClass {
String title;
String desc;
String[] photos;
public MyClass(){}
public MyClass(String title, String desc, String[] photos) {
this.title = title;
this.desc = desc;
this.photos = photos;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String[] getPhotos() {
return photos;
}
public void setPhotos(String[] photos) {
this.photos = photos;
}
我在MyClass上收到此错误obj = document.toObject(MyClass.class) :
FATAL EXCEPTION: main
Process: com.developtrainmaintain.firebaserecycle, PID: 3836
java.lang.RuntimeException: Could not deserialize object. Converting to Arrays is not supported, please use Lists instead (found in field 'photos')
at com.google.android.gms.internal.firebase-firestore.zzko.zzb(Unknown Source)
at com.google.android.gms.internal.firebase-firestore.zzko.zza(Unknown Source)
at com.google.android.gms.internal.firebase-firestore.zzko.zza(Unknown Source)
at com.google.android.gms.internal.firebase-firestore.zzko.zzb(Unknown Source)
at com.google.android.gms.internal.firebase-firestore.zzko$zza.zza(Unknown Source)
at com.google.android.gms.internal.firebase-firestore.zzko.zza(Unknown Source)
at com.google.android.gms.internal.firebase-firestore.zzko.zza(Unknown Source)
at com.google.firebase.firestore.DocumentSnapshot.zza(Unknown Source)
at com.google.firebase.firestore.QueryDocumentSnapshot.zza(Unknown Source)
at com.google.firebase.firestore.DocumentSnapshot.toObject(Unknown Source)
at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(Unknown Source)
at com.developtrainmaintain.firebaserecycle.MainActivity$1.onComplete(MainActivity.java:48)
at com.google.android.gms.tasks.zzj.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
请帮我解决这个问题:(
答案 0 :(得分:1)
您看到的错误消息是:
无法反序列化对象。 com.developtrainmaintain.firebaserecycle.MyClass类没有定义无参构造函数。如果您使用的是ProGuard,请确保这些构造函数未被剥离
所有JavaBean类型对象都应该有一个no-arg构造函数,因此可以创建它们的新实例,而不必猜测如何在构造函数中传递参数。所以在你的类中添加一个no-arg构造函数:
public MyClass() {}
答案 1 :(得分:0)
由于您在类中提供了参数化构造函数,因此默认情况下没有参数构造函数不再可用。定义一个无参数构造函数,然后它应该工作
package com.mikadosolutions.training.spring.di;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class LaunchTraining
{
public static void main(String args[])
{
ApplicationContext context = new FileSystemXmlApplicationContext("TrainingConfig.xml");
Workshop trainingWorkshop = (Workshop) context.getBean("trainingWorkshop");
trainingWorkshop.conductWorkshop();
}
}
答案 2 :(得分:0)
public class MyClass {
String title;
String desc;
ArrayList<String> photos;
public MyClass(){}
public MyClass(String title, String desc, ArrayList<String> photos) {
this.title = title;
this.desc = desc;
this.photos = photos;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public ArrayList<String> getPhotos() {
return photos;
}
public void setPhotos(ArrayList<String> photos) {
this.photos = photos;
}
}
Firebase或FireStore不支持使用列表或映射类而不是数组