- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
JSON结构:
{
"breviario": {
"metaLiturgia": {
"fecha" : "Martes 5 de febrero del 2019",
"tiempo" : "PROPIO DE LOS SANTOS",
"semana" : "",
"mensaje": "",
"salterio": "",
"color":0,
"meta": ""
},
"santo": {
"nombre": "Santa Águeda, virgen y mártir",
"vida": "Padeció el martirio en Catania (Sicilia), probablemente en la persecución de Decio. Desde la antigüedad su culto se extendió por toda la Iglesia y su nombre fue introducido en el Canon romano."
},
"oficio": {
"himno": {
"texto": "Testigos de amor~de Cristo Señor,~mártires santos.§Rosales en flor~de Cristo el olor,~mártires santos.§Palabras en luz~de Cristo Jesús,~mártires santos.§Corona inmortal~del Cristo total,~mártires santos. Amén."
},
"salmodia": {
...
Oficio
:
public class Oficio {
private Invitatorio invitatorio;
private Himno himno;
private Salmodia salmodia;
private String oracion;
private String responsorio;
private OficioLecturas oficioLecturas;
public Oficio () {}
public Himno getHimno() {
return himno;
}
public void setHimno(Himno himno) {
this.himno = himno;
}
// ...
}
Himno
:
public class Himno {
private String texto;
public Himno () {}
public Spanned getTexto() {
Spanned str = Utils.fromHtml(Utils.getFormato(texto));
return str;
}
public void setTexto(String texto) {
this.texto = texto;
}
//...
}
DocumentReference docRef = db.collection("liturgia").document("breviario")
.collection("oficio").document("20190204");
docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
Oficio oficio = documentSnapshot.toObject(Oficio.class);
Himno himno=oficio.getHimno();
Log.d(TAG,oficio.getOracion().toString()); //Correct
}
});
himno
作为自定义类“Himno”。当我尝试时,即使我评论以下行,我也会收到“RuntimeException”:
Himno himno=oficio.getHimno();
.但是我可以得到属性
oracion
到对应的变量中。
E/AndroidRuntime: FATAL EXCEPTION: main Process: org.my.app, PID: 10532 java.lang.RuntimeException: Could not deserialize object. Can't convert object of type com.google.firebase.firestore.DocumentReference to type org.my.app.model.Himno (found in field 'himno') at com.google.firebase.firestore.util.CustomClassMapper.deserializeError(com.google.firebase:firebase-firestore@@17.1.2:524) at com.google.firebase.firestore.util.CustomClassMapper.convertBean(com.google.firebase:firebase-firestore@@17.1.2:505) at com.google.firebase.firestore.util.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-firestore@@17.1.2:242) at com.google.firebase.firestore.util.CustomClassMapper.deserializeToType(com.google.firebase:firebase-firestore@@17.1.2:180) at com.google.firebase.firestore.util.CustomClassMapper.access$200(com.google.firebase:firebase-firestore@@17.1.2:53) at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-firestore@@17.1.2:700) at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-firestore@@17.1.2:674) at com.google.firebase.firestore.util.CustomClassMapper.convertBean(com.google.firebase:firebase-firestore@@17.1.2:503) at com.google.firebase.firestore.util.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-firestore@@17.1.2:242) at com.google.firebase.firestore.util.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-firestore@@17.1.2:97) at com.google.firebase.firestore.DocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@17.1.2:203) at com.google.firebase.firestore.DocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@17.1.2:183)
最佳答案
您收到以下错误:
Can't convert object of type com.google.firebase.firestore.DocumentReference to type org.my.app.model.Himno (found in field 'himno')
DocumentReference
反对
Himno
目的。 Java 中没有办法实现这一点,因为它们之间没有继承关系。
db.collection("liturgia").document("breviario").collection("oficio").document("20190204");
Oficio
,所以下面的代码行:
Oficio oficio = documentSnapshot.toObject(Oficio.class);
Himno
嵌套在您的
Oficio
下的对象像这样的类:
Himno himno=oficio.getHimno();
himno
文档中的属性包含一个类型为
DocumentReference
的值和
不是 类型
Himno
.
himno
属性有引用。如果您想获得该文档引用,只需使用:
DocumentReference documentReference = documentSnapshot.getDocumentReference("himno");
Himno himno=oficio.getHimno();
himno
属性类型为
Himno
而不是
DocumentReference
.
getTexto()
方法应返回
String
而不是
Spanned
对象,因为这是存储在数据库中的方式。
texto
属性包含一个字符串而不是
Spanned
.但这是
不是 你的错误发生的原因。
Custom classes in Firestore must contain
public class Oficio {
public Invitatorio invitatorio;
public Himno himno;
public Salmodia salmodia;
public String oracion;
public String responsorio;
public OficioLecturas oficioLecturas;
}
I changet the type of getTexto() from Spanned to String, but is not working.
getTexto()
的返回类型来自
Spanned
的方法至
String
不会帮助您解决主要错误。
I don't understand how i can change the himno property to be of type Himno and not DocumentReference.
himno
这些文件中的属性(property)。然后,当您添加新文档时,而不是添加
DocumentReference
, 添加
Himno
类型的对象,正如我还看到您在
Oficio
中声明的那样类(class)。这也意味着,当您将使用以下代码行时:
Himno himno=oficio.getHimno();
Himno
类型的对象将被退回而不是
DocumentReference
.这是因为它是根据其数据类型存储的。
In RealTimeDatabase is possible to take only one object (Oficio par example) and from this object i can get references to another nested object (Himno par example).
DocumentReference
,您无法将其作为类型
Himno
,除非你像这样添加它。
See the section named "Custom objects" in the docs
DocumentReference
,照着拿。如果你想得到它作为
Himno
, 将其存储为
Himno
首先,然后相应地得到它。
Are you telling me that in can create a field of type Himno in Firestore?
Himno
目的。
I don't understand how i can achieve this.
Oficio
类型的对象向数据库添加数据时,设置
Himno
以正确的方式反对。尝试这个:
Oficio oficio = new Oficio();
Himno himno = new Himno();
himno.setTexto("Your Text");
oficio.setHimno(himno);
docRef.set(oficio);
I'm misunderstanding things about it?
关于java - "RuntimeException: Could not deserialize object"从 Firestore 读取嵌套对象时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54534278/
我真的很难弄明白这一点。当我查看 logcat 指向错误的位置时,它指向这一行: throw new RuntimeException(i); 在里面 public void onDetach() {
@Transactional (noRollbackFor=RuntimeException.class) public void methodA (Entity e){ service.met
我正在开发简历应用程序并遇到以下异常 在gradle文件上,我尝试了所有可能的解决方案,使缓存重启无效,并且干净的gradle不起作用 java.lang.RuntimeException:java.
我的项目在 Android Studio 3.0.1 上的 Whatchap 中,这是我的设计 gradle。build.gradle(应用程序) 应用插件:'com.android.applicat
// Top-level build file where you can add configuration options common to all sub-projects/modules.
您好,目前我正在开发包括解析服务器的项目,并且在将解析库添加到我的项目后,当我运行它时,它显示此错误,任务“:app:checkDebugDuplicateClasses”执行失败。 Exec
我找到了 another post显示我们如何创建我们自己的检查异常,它也返回不同于 500 的 HTTP 状态代码。但是,我需要它是一个 RuntimeException。然后,我找到了WebApp
我尝试在 7.40 系统上演示具有动态方法名称的 CALL METHOD 语句。我使用下面的测试代码,在第 27 行得到一个 ABAP 运行时错误。异常描述中的错误分析状态 ... 在类 LCL 中,
我的ViewModel类抛出了RunTimeException。它基本上不能创建ViewModel类的实例。这是堆栈跟踪: 2019-05-29 01:27:56.700 9698-9698/com.
我安装了Hive 0.9.0,运行它时出现错误: ./hive Logging initialized using configuration in jar:file:/usr/lib/hive-0.
当我尝试在 android 4.1.1(galaxy nexus)上运行 opencv(2.4.2) sample3时。我收到RuntimeException。 Logcat指向此处:(android
Java中除了RuntimeException之外还有其他可能发生的异常吗?谢谢。 最佳答案 是的,有三种种。 检查异常 编译器会让您知道何时可以抛出它们,很可能是由于环境中的故障。 如果程序可以
这个问题已经有答案了: Why runtime exception is unchecked exception? (5 个回答) 已关闭 7 年前。 我知道受检查的异常继承自 Exception并且
这里发生了什么? 为什么 IOException (RemoteException) 的检查子级会转换为 RuntimeException? 摘自here的片段 import java.rmi.Rem
源代码(osmand)没有错误 我使用了 eclipse 和 sdk 模拟器 2.2,从 googlecode checkout (osmand应用程序) 我的代码: public void onCr
我开始研究线程,我不明白为什么下面的简单代码不起作用。它正在抛出: RuntimeException: can't create handler inside thread that has not
我想为自己破例。但是当我抛出异常时,程序就关闭了。那么我怎样才能运行我的方法3次然后关闭程序呢? (我的程序在intArray之后关闭,但我想遍历dobleArray和charArray) 这是我的异
我有一个扩展 HttpServlet 的类包含以下两个方法。当客户端向 Tomcat 服务器上运行的 Web 应用程序发出请求时,http 请求将通过此类处理,但是如果 doSomething 方法抛
在很多地方,我都会遇到几个检查异常,例如 IOException、ParseException、JSONException 等。我必须做出两个选择 - 通过在方法签名末尾添加 throws 来引发相同
RuntimeExceptions 应该指示编程错误,我希望我的应用程序在我的可观察对象中的某些东西抛出 RuntimeException 时崩溃。 最好的方法是什么?现在我正在考虑这个解决方案(它是
我是一名优秀的程序员,十分优秀!