2012-02-09 47 views
2

我很难尝试序列化我的课程(从字面上看3-4小时才能找到解决方案)。我一个子类添加到现有的序列化和功能类,并比收到以下错误信息:GWT +系列化

[ERROR] com.google.gwt.user.client.ui.DelegatingChangeListenerCollection is not default instantiable (it must have a zero-argument constructor or no constructors at all) and has no custom serializer. (reached via com.client.rpc.ItemRecRpc) 
       [ERROR] com.google.gwt.user.client.ui.DelegatingClickListenerCollection is not default instantiable (it must have a zero-argument constructor or no constructors at all) and has no custom serializer. (reached via com.client.rpc.ItemRecRpc) 
       [ERROR] com.google.gwt.user.client.ui.DelegatingFocusListenerCollection is not default instantiable (it must have a zero-argument constructor or no constructors at all) and has no custom serializer. (reached via com.client.rpc.ItemRecRpc) 
       [ERROR] com.google.gwt.user.client.ui.DelegatingKeyboardListenerCollection is not default instantiable (it must have a zero-argument constructor or no constructors at all) and has no custom serializer. (reached via com.client.rpc.ItemRecRpc) 
       [ERROR] com.google.gwt.view.client.ListDataProvider<T>.ListWrapper<> is not default instantiable (it must have a zero-argument constructor or no constructors at all) and has no custom serializer. (reached via com.client.rpc.ItemRecRpc) 
       [ERROR] com.client.rpc.ItemRecRpc.LogCollection is not default instantiable (it must have a zero-argument constructor or no constructors at all) and has no custom serializer. (reached via com.client.rpc.ItemRecRpc) 
       [ERROR] com.client.rpc.ItemRecRpc.LogCollection has no available instantiable subtypes. (reached via com.client.rpc.ItemRecRpc) 
       [ERROR] subtype com.client.rpc.ItemRecRpc.LogCollection is not default instantiable (it must have a zero-argument constructor or no constructors at all) and has no custom serializer. (reached via com.client.rpc.ItemRecRpc) 
       [ERROR] java.util.AbstractList.SubList<E> is not default instantiable (it must have a zero-argument constructor or no constructors at all) and has no custom serializer. (reached via com.client.rpc.ItemRecRpc) 
       [ERROR] java.util.Collections.UnmodifiableList<T> is not default instantiable (it must have a zero-argument constructor or no constructors at all) and has no custom serializer. (reached via com.client.rpc.ItemRecRpc) 
       [ERROR] java.util.Collections.UnmodifiableRandomAccessList<T> is not default instantiable (it must have a zero-argument 

我班的样子:

public class ItemRecRpc implements Serializable { 

    private static final long serialVersionUID = -5828108890651522661L; 
     . 
     . 

     private String rId; 
    private LogCollection logColl;//if i comment this, no error message... 

    public class LogCollection{ 

     public LogCollection(){ 

     } 
     //public long creationTime = System.currentTimeMillis(); 
     //public LongVector times = new LongVector(); 
     //public ArrayList<Object> messages = new ArrayList<Object>(); 
     //public int nSkipped = 0; 
     //public int nExceptions = 0; 
     //public Exception firstException = null; 
     //public long endGcTime=0; 
     public long endTime; 
    } 
. 
. 
. 
} 

当我评论了“私人LogCollection logColl”行之是好的,但是当我取消注释我再次得到错误消息。我试着用static关键字,当你看到我的评论每个子类中的变量,但不能帮助...反正如果我创建一个新类:

public class LogCollectionRpc implements Serializable { 

    public LogCollectionRpc() { 
     // 
    } 
    public long creationTime = System.currentTimeMillis(); 
    public LongVector times = new LongVector(); 
    public ArrayList<Object> messages = new ArrayList<Object>(); 
    public int nSkipped = 0; // due to reaching the limit 
    public int nExceptions = 0; // due to MyAppender-s 
    public Exception firstException = null; // due to MyAppender-s 
    public long endGcTime = 0; 
    public long endTime; 

} 

而不是试图用这个作为我的功能类,它是好吧......但这件事真的让我心烦......

有什么想法吗? Gwt不支持子类序列化?或者我想念一些东西。赞扬任何答案。

最好的问候, 彼得

+1

这就是@jusio所说的:内部非静态类需要一个封闭实例,所以它们的构造函数会收到一个额外的参数(编译时)。这就是为什么它抱怨没有零参数构造函数。在这种情况下,我认为最合适的是将LogCollection声明为“公共静态类”,以使其与封闭类型无关。或者直接将其移至自己的新文件。删除'private LogCollection ...'字段的原因是GWT意识到它不需要子类型并忽略它。 – helios 2012-02-09 14:09:06

+0

是真的,但它看起来也是比较成功,但如果我写“实现可序列化”的GWT不再抱怨了,所以静态和实现也是不可抱怨的。奇怪是不是? – czupe 2012-02-09 14:32:57

回答

7

此错误:

subtype com.client.rpc.ItemRecRpc.LogCollection is not default instantiable

说,它不能创建和LogCollection实例默认。这是真的。由于要创建LogCollection的实例,您首先需要有一个ItemRecRpc的实例。声明LogCollection静态类应该有所帮助。

基本上,当你想通过gwt-rpc发送一些对象时,所有在这样的对象中用作字段的类都应该是默认实例化的。 (例如,没有特别的技巧来创建它,只是新的和空的构造函数)。您也可以为任何类定义一个自定义字段序列化程序,可以默认实例化。

+0

完美答案,谢谢。它解决了我的问题。 (我尝试过使用静态,但有太多的项目,没有建立一个合适的:/ facepalm :(但现在如你所告诉我的,我专注并重建合适的项目,这是好事!) – czupe 2012-02-09 14:07:08

+0

好的测试,它,并最后评论失败:(仍然有相同的消息,真的很奇怪,也许它是一个GWT问题seriulesy ...你不能让任何类的子类实现seriazable ...我试图克服这个错误... “公共静态类LogCollection” – czupe 2012-02-09 14:20:52

+0

好吧也许它有一个解决方案: “公共静态类LogCollection实现Serializable” 不仅需要静态的,而是需要实现Seriazable藏汉...真正奇怪的事情这个,因为我知道一个Serializable类的子类也实现了Serializable(不添加显式),但不是GWT,因为我看到...真的很奇怪...但在这是静态,并实现关键字没有错误消息... – czupe 2012-02-09 14:30:36