首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2ME开发 >

初学急求(待高手指导)java.lang.InstantiationException: Class not a MIDlet?已经写了MIDlet类,如何修改

2011-12-11 
初学急求(在线等待高手指导)java.lang.InstantiationException: Class not a MIDlet?已经写了MIDlet类,怎

初学急求(在线等待高手指导)java.lang.InstantiationException: Class not a MIDlet?已经写了MIDlet类,怎么修改!
ExampleGameSpriteMIDlet代码:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ExampleGameSpriteMIDlet extends MIDlet implements CommandListener
{
 private ExampleGameSprite gameCanvas; //声明游戏画布对象
 private Command exitCmd=new Command("Exit",Command.SCREEN,1); //创建退出软键
 
 public ExampleGameSpriteMIDlet()
 {
  gameCanvas=new ExampleGameSprite(); //创建游戏画布对象
  gameCanvas.addCommand(exitCmd); //向游戏画布添加退出软键
  gameCanvas.setCommandListener(this); //侦听软键事件
 }
 
 public void startApp()
 {
  gameCanvas.start(); //启动游戏线程
  Display.getDisplay(this).setCurrent(gameCanvas); //在屏幕上显示游戏画布
 }
 
 public void pauseApp()
 {
 }
 
 public void destroyApp(boolean unconditional)
 {
 }
 
 public void commandAction(Command cmd,Displayable disp)
 {
  if(cmd==exitCmd) //如果用户按下退出软键,则推出程序
  {
  System.gc();
  destroyApp(false);
  notifyDestroyed();
  }
 }
}

class ExampleGameSprite代码:
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class ExampleGameSprite extends GameCanvas implements Runnable

{
  
  private boolean isPlay; //isPlay标志位决定游戏线程是否运行
  private int Width,Height; //屏幕的宽度和高度
  private final static int STEP=3; //人物移动幅度
  private Sprite hero=null; //游戏精灵
  private int frameWidth,frameHeight; //精灵的帧宽度和高度
  private int lastState=-1; //游戏动作的前一状态,用来设置当前帧
  public ExampleGameSprite()
  
  {
   
  super(true); //抑制游戏键盘
  Width=getWidth(); //获取屏幕宽度
  Height=getHeight(); //获取屏幕高度
   
  try
  {
   
  Image img=Image.createImage("/sprite.png"); //加载图片资源,sprite.png大小是72(宽)X 128(高)
  hero=new Sprite(img,24,32); //创建精灵
  frameWidth=24; //帧宽度为24象素
  frameHeight=32; //帧高度为32象素
   
  }
   
  catch(Exception e)
  {
   
  e.printStackTrace(); //捕捉异常
   
  }
 
  }
  
  public void start()
  {
   
  isPlay=true;
  Thread t=new Thread(this); //创建游戏线程
  t.start(); //启动游戏线程

  }
 
  public void stop() {isPlay=false;} //停止游戏循环

  public void run()
  { //游戏循环

  Graphics g=getGraphics(); //获取Graphics对象
  int timeStep=100; //设定游戏循环执行一次所耗费的时间
   
  while(isPlay) //while(isPlay==true)


  {
   
  long start=System.currentTimeMillis(); //获取线程起始时间
  input(); //获取用户输入
  draw(g); //绘制屏幕
  long end=System.currentTimeMillis(); //获取执行完毕的事件
  int duration=(int)(end-start);
   
  if(duration<timeStep) //若执行时间小于循环预定时间,则等待到循环时间
  {
   
  try
  {
  Thread.sleep(timeStep-duration); //让线程等待
  }
   
  catch(InterruptedException ie){}
  }
  }
  } 
  
  private void input()
  {
  int keyStates=getKeyStates(); //获取键盘信息
  if( (keyStates & LEFT_PRESSED)!=0) //若用户按下游戏左键
  {
   
  if(lastState!=LEFT_PRESSED) //若前一状态不是向左
  {
   
  lastState=LEFT_PRESSED;
  hero.setFrameSequence(new int[]{3,4,5}); //设置精灵向左的帧序列
   
  }
  else
  {
   
  hero.nextFrame(); //精灵自动转下一帧
   
  }
   
  if(hero.getX()-STEP>=0)
  hero.move(-STEP,0); //精灵向左移动
   
  else
  hero.setPosition(0,hero.getY()); //若精灵到达左边界则保持不动
  }

 

 if((keyStates & RIGHT_PRESSED)!=0) //若用户按下右键
  {

  if(lastState!=RIGHT_PRESSED) //若前一状态不是向左
  {
 
  lastState=RIGHT_PRESSED;
  hero.setFrameSequence(new int[] {9,10,11}); //设置精灵向右的帧序列
  
  }
  
  else
  {
  hero.nextFrame(); //精灵转下一帧
  }

  
  if(hero.getX()+frameWidth<Width)
  hero.move(STEP,0); //精灵向右移动
  
  else
  hero.setPosition(Width-frameWidth,hero.getY()); //若精灵到达右边界则保持不动
 
 }


 if((keyStates & UP_PRESSED)!=0) //若用户按下向上的游戏键盘
 {

  if(lastState!=UP_PRESSED) //若前一状态不是向上
  {
   
  lastState=UP_PRESSED;
  hero.setFrameSequence(new int[] {6,7,8}); //设置精灵向上的帧序列
 
  }
 
 else
  { 
  hero.nextFrame(); //精灵跳转下一帧
  }


  if(hero.getY()-STEP>=0)
  hero.move(0,-STEP); //精灵向上移动
 
  else
  hero.setPosition(hero.getX(),0); //若精灵到达上边界则停止不动
 }


 if((keyStates & DOWN_PRESSED)!=0) //若用户按下向下的游戏键盘
 {
  if(lastState!=DOWN_PRESSED)
  {
  lastState=DOWN_PRESSED;
  hero.setFrameSequence(new int[] {0,1,2}); //设置精灵向下的帧序列
  }
  else
  {
  hero.nextFrame(); //精灵自动下一帧
  }
  
  if(hero.getY()+frameHeight<Height)
  hero.move(0,STEP); //精灵向下移动
  else
  hero.setPosition(hero.getX(),Height-frameHeight); //若到达下边界则精灵停止移动
 }


 if(keyStates==0) hero.setFrame(0); //若没有用户输入。则自动跳到帧序列的第0帧


  
}

 
 private void draw(Graphics g)  
 {

  g.setColor(0xffffff);
  g.fillRect(0,0,Width,Height); //填充屏幕
  hero.paint(g); //绘制精灵
  flushGraphics(); //将缓冲屏幕上的内容绘制到手机屏幕上
 }
}

运行后出现错误:
Project settings saved
Building "ExampleGameSprite"
Build complete
Running with storage root DefaultColorPhone
Unable to create MIDlet ExampleGameSprite
java.lang.InstantiationException: Class not a MIDlet
at com.sun.midp.midlet.MIDletState.createMIDlet(+51)
at com.sun.midp.midlet.Selector.run(+22)
哪为大虾帮帮忙下,先谢谢了!!

[解决办法]
Unable to create MIDlet ExampleGameSprite 
java.lang.InstantiationException: Class not a MIDlet 

ExampleGameSprite不是MIDlet的子类
用ExampleGameSpriteMIDlet来运行的你的程序

热点排行