zoukankan      html  css  js  c++  java
  • No-args constructor for class does not exist. Register an InstanceCreator with G

    有时候我们在使用Googel官方的json解析包时,如果自己的实体类中出现代参的构造函数.在1.4的jar中,如果类造型中有参数,就会调用不了无参构造器,(如:HashMap的构造器就会有参数)

    参考博文内容:

    有时候我们在使用Googel官方的json解析包时,如果自己的实体类中出现代参的构造函数,如; 

    public class InformationSummary { 
        /** 
         * id 
         */ 
        public String id; 

        /** 
         * 栏目名称 
         */ 
        public String name; 

        /** 
         * 栏目图片 
         */ 
        public String picture; 

        /** 
         * 栏目内容 
         */ 
        public String content; 

        /** 
         * 新闻头条id 
         */ 
        public String newsId; 

        /** 
         * 子业务编码 
         */ 
        public String subCode; 
        
       

        public InformationSummary(String id, String name, String picture, String content) { 
            super(); 
            this.id = id; 
            this.name = name; 
            this.picture = picture; 
            this.content = content; 
        } 
    如果这样写的话,有时候会报这样的错误; 

    06-26 17:08:59.723: W/System.err(4724): java.lang.RuntimeException: No-args constructor for class com.funo.health.bean.business.InformationSummary does not exist. Register an InstanceCreator with Gson for this type to fix this problem. 
    06-26 17:08:59.723: W/System.err(4724): at com.google.gson.MappedObjectConstructor.constructWithNoArgConstructor(MappedObjectConstructor.java:64) 
    简单认识就是我们自己写了构造函数后,本身自带的无参就会忽略,但是json.jar包中的Gson,需要!简单方法就是加上午参数构造函数! 

    参考地址: http://www.tuicool.com/articles/v6b2Mb

    http://javaeedyc.iteye.com/blog/1894799

  • 相关阅读:
    HashMap实现原理
    设计模式-2-代理模式
    设计模式-1-单例模式
    重构技巧
    unable to create new native thread
    设计模式-6大原则
    阿里前端在线编程题
    如何实现用户懒加载?
    《我敢活成我想要的样子》读后感
    改bug心得
  • 原文地址:https://www.cnblogs.com/pyfreshman/p/4843268.html
Copyright © 2011-2022 走看看