日期:2014-05-20  浏览次数:20812 次

有会的快快告诉我,JAVA对象序列化问题,急啊!
我自定义了个类class Message implements Serializable { 
Image im; 
Message(Image im){ 
this.im=im; 


也实现了Serializable 接口;但是这个类里面有个Image对象,不能序列化,怎么解决? 

问题补充: 
Image off_screen_buf; 
if (off_screen_buf == null) { 
off_screen_buf = this.createImage(1024, 768); 
msg = new Message( off_screen_buf); 
这样做不行的,具体点的! 
out.writeObject(msg);的时候会抛异常: 
java.io.NotSerializableException: sun.awt.image.OffScreenImage  
(我只是想做一个网络绘画板,画板用的双缓冲,所以想直接把off_screen_buf传给其他人,但是在网络上传输的时候遇到了现在这个问题,off_screen_buf不能序列化,应该怎么解决,谢谢!)

------解决方案--------------------
把Image im; 加上关键字transient
然后自己定义2个方法:
private void writeObject(ObjectOutputStream stream) throws IOException;
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException;
------解决方案--------------------
private void writeObject(ObjectOutputStream s) throws IOException {
......//其他处理

对Image的处理
int[] pixels = image != null? new int[w * h] : null;
if (image != null) {
try {
PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
pg.grabPixels();
if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
throw new IOException("failed to load image contents");
}
}
catch (InterruptedException e) {
throw new IOException("image load interrupted");
}
}
s.writeObject(pixels);
}
------解决方案--------------------
引用楼主 jdk123987 的帖子:
我自定义了个类class Message implements Serializable {
Image im;
Message(Image im){
this.im=im;
}
}
也实现了Serializable 接口;但是这个类里面有个Image对象,不能序列化,怎么解决?

问题补充:
Image off_screen_buf;
if (off_screen_buf == null) {
off_screen_buf = this.createImage(1024, 768);
msg = new Message( off_screen_buf);
这样做不行的,具体点的!
out.writeObject(msg);的时候会…