在使用writeobject之前没有错误,但它一直没有给我有效的构造函数错误,即使我创建了no-arg构造函数和UID,我的编码中出现了什么问题?
这是控制台中的错误代码
java.io.InvalidClassException: AirSwift.Game_player$Player; no valid constructor
at java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(Unknown Source)
at java.io.ObjectStreamClass.checkDeserialize(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at AirSwift.Game_stage.GameLoad(Game_stage.java:168)
at AirSwift.Game_main$7.actionPerformed(Game_main.java:168)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
这里的一个类实现了Externalizable
public class Player implements Externalizable {
/**
*
*/
private static final long serialVersionUID = 1L;
public int x, y, mode, attackspeed, speed, speedinattack, firecount, hp, mp, time;
public boolean modechanging = false;
public int modechangecount = 0;
Image Player_img = new ImageIcon("Images/Player/Player_0.png").getImage();
Image []Player_Mode_img;
String sound_fire, sound_change;
public Player(){
}
public Player(int hp){
this.ChangeMode(1);
this.hp = hp;
this.mp = 100;
this.x = 100;
this.y = 600;
this.time = 0;
}
Image AnimeMode(){
switch(mode){
case 1:
if(modechangecount < 10) return Player_Mode_img[0];
else if((9<modechangecount)&&(modechangecount<20)) return Player_Mode_img[1];
else return Player_Mode_img[2];
case 2:
return Player_Mode_img[0];
case 3:
if(modechangecount < 10) return Player_Mode_img[0];
else if((9<modechangecount)&&(modechangecount<20)) return Player_Mode_img[1];
else return Player_Mode_img[2];
default: return default_img;
}
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
// TODO Auto-generated method stub
this.x = in.readInt(); this.y = in.readInt(); this.hp = in.readInt(); this.time = in.readInt();
}
public void writeExternal(ObjectOutput out) throws IOException {
// TODO Auto-generated method stub
out.writeInt(this.x); out.writeInt(this.y); out.writeInt(this.hp); out.writeInt(this.time);
}
}
答案 0 :(得分:2)
将默认构造函数添加到AirSwift.Game_player
或将Player
声明为public static