当前位置: 代码迷 >> J2ME >> Canvas画布屏幕设计图形动画出错,该怎么处理
  详细解决方案

Canvas画布屏幕设计图形动画出错,该怎么处理

热度:7499   发布时间:2013-02-25 21:33:34.0
Canvas画布屏幕设计图形动画出错
我用Canvas画布屏幕设计来图形动画出现了错误,希望看看错在哪里?代码如下
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;


public class showpicture extends MIDlet implements CommandListener{
  public Display display;
  private Mycanvas mycanvas;
  private Command cmd_exit;
public showpicture() {
display=Display.getDisplay(this);
cmd_exit=new Command("Exit",Command.EXIT,1);
mycanvas = new Mycanvas();
mycanvas.addCommand(cmd_exit);
mycanvas.setCommandListener(this);

}

protected void destroyApp(boolean arg0) {
// TODO 自动生成方法存根

}

protected void pauseApp() {
// TODO 自动生成方法存根

}

protected void startApp() {

  display.setCurrent(mycanvas);
  Thread tr=new Thread(mycanvas);
  tr.start();
   
}

public void commandAction(Command arg0, Displayable arg1) {
if(arg0==cmd_exit)
{
destroyApp(false);
notifyDestroyed();

}

}

}
class Mycanvas extends Canvas implements Runnable{
  private Image image[]=new Image[5];
  private int currentFrame=0;
private Display display;
  public Mycanvas()
  {
  try{
  for(int i=0;i<5;i++)
  {
  image[i]=Image.createImage("/"+i+".jpeg");
  }
  }catch(Exception e){}
  currentFrame=0;
 
 
 
  }
protected void paint(Graphics g) {
g.setColor(255, 255, 255);
g.fillRect(0,0, getWidth(),getHeight());
g.drawImage(image[currentFrame], getWidth()/2,getHeight()/2, g.HCENTER|g.VCENTER);


}

public void run() {
try{
Thread.sleep(200);
currentFrame=(currentFrame+1)%5;
repaint();
  
  display.callSerially(this);

}catch(Exception ce){}
}



}
错误为:
java.lang.NullPointerException
at Mycanvas.paint(+59)
at javax.microedition.lcdui.Canvas.callPaint(+85)
at javax.microedition.lcdui.Display.repaint(+82)
at javax.microedition.lcdui.Display.registerNewCurrent(+235)
at javax.microedition.lcdui.Display.access$700(+6)
at javax.microedition.lcdui.Display$DisplayAccessor.foregroundNotify(+46)
at javax.microedition.lcdui.Display$DisplayManagerImpl.notifyWantsForeground(+152)
at javax.microedition.lcdui.Display$DisplayManagerImpl.access$100(+6)
at javax.microedition.lcdui.Display.setCurrent(+70)
at showpicture.startApp(+11)
at javax.microedition.midlet.MIDletProxy.startApp(+7)
at com.sun.midp.midlet.Scheduler.schedule(+270)
at com.sun.midp.main.Main.runLocalClass(+28)
at com.sun.midp.main.Main.main(+80)
java.lang.NullPointerException
at Mycanvas.paint(+59)
at javax.microedition.lcdui.Canvas.callPaint(+85)
at javax.microedition.lcdui.Display.repaint(+82)
at javax.microedition.lcdui.Display$DisplayAccessor.repaint(+14)
at javax.microedition.lcdui.Display$DisplayManagerImpl.repaint(+16)
at com.sun.midp.lcdui.DefaultEventHandler.repaintScreenEvent(+24)
at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+519)
Execution completed.


------解决方案--------------------------------------------------------
看看创建图片有没有错误.捕获一下.应该是画图片的时候报的空指针
------解决方案--------------------------------------------------------
应该是这个模拟器不支持jpeg格式吧
------解决方案--------------------------------------------------------
package core;

import javax.microedition.lcdui.Canvas;