2016-11-15 42 views
-1

直到我使用writeobject之前没有错误,但是它始终不给我有效的构造函数错误,即使我创建了无参数构造函数和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); 
     } 
    } 

回答

2

添加默认的构造函数来AirSwift.Game_player以及或声明Playerpublic static