Firebase抛出No properties to serialize found on class错误如何解决
问题根因
Firebase 序列化/反序列化机制仅识别类的非静态实例属性,你提供的NewsModel中所有字段、getter、setter方法均被static关键字修饰,属于类级别的共享属性,不属于实例对象的独有属性,因此序列化时无法扫描到有效可序列化的属性,触发对应报错。
解决方案
移除所有字段、getter、setter方法的static修饰符即可,修正后的代码如下:
public class NewsModel implements Serializable { public String Title; public String Body; public String Date; public String ID; public String Location; public String Source; public NewsModel(String title, String body, String date, String ID, String location, String source) { Title = title; Body = body; Date = date; this.ID = ID; Location = location; Source = source; } public NewsModel(){ } public String getTitle() { return Title; } public void setTitle(String title) { Title = title; } public String getBody() { return Body; } public void setBody(String body) { Body = body; } public String getDate() { return Date; } public void setDate(String date) { Date = date; } public String getID() { return ID; } public void setID(String ID) { this.ID = ID; } public String getLocation() { return Location; } public void setLocation(String location) { Location = location; } public String getSource() { return Source; } public void setSource(String source) { Source = source; } }
额外注意事项
- 需保留public无参构造方法,该方法是Firebase反序列化实例化对象的必要条件
- 如果字段命名和Firebase中存储的key不匹配,可给对应字段添加
@PropertyName("Firebase中对应的key名")注解显式映射关系 - 不要给序列化类的字段、存取方法添加static、transient等会影响属性读写的修饰符
内容的提问来源于stack exchange,提问作者Sathishkumar G
相关产品推荐
相关产品推荐

