最近在学J2ME游戏基础,刚学到做一个简单的动画精灵就遇到问题了,很郁闷~!
请问下面代码哪里出问题了,我跟着视频教程一步步写的,为什么我异常,而人家却可以运行?????
异常为:
Running with storage root C:\Documents and Settings\Administrator\j2mewtk\2.5.2\appdb\DefaultColorPhone
Running with locale: Chinese_People's Republic of China.936
Running in the identified_third_party security domain
java.lang.IllegalArgumentException
at javax.microedition.lcdui.game.Sprite.<init>(+41)
at spritepengzhuang.MIDlet4$MyGameCanvas.<init>(+60)
at spritepengzhuang.MIDlet4.<init>(+13)
at java.lang.Class.runCustomCode(+0)
at com.sun.midp.midlet.MIDletState.createMIDlet(+34)
at com.sun.midp.midlet.Scheduler.schedule(+52)
at com.sun.midp.main.Main.runLocalClass(+28)
at com.sun.midp.main.Main.main(+80)
java.lang.NullPointerException
at spritepengzhuang.MIDlet4$MyGameCanvas.<init>(+77)
at spritepengzhuang.MIDlet4.<init>(+13)
at java.lang.Class.runCustomCode(+0)
at com.sun.midp.midlet.MIDletState.createMIDlet(+34)
at com.sun.midp.midlet.Scheduler.schedule(+52)
at com.sun.midp.main.Main.runLocalClass(+28)
at com.sun.midp.main.Main.main(+80)
- Java code
//动画的精灵同时也在运动,把一个连续的图片分成几个等分部分,import javax.microedition.lcdui.Display;import javax.microedition.lcdui.Graphics;import javax.microedition.lcdui.Image;import javax.microedition.lcdui.game.GameCanvas;import javax.microedition.lcdui.game.Sprite;import javax.microedition.midlet.MIDlet;import javax.microedition.midlet.MIDletStateChangeException;public class MIDlet4 extends MIDlet { private MyGameCanvas mgc=new MyGameCanvas(); private Display dis; public MIDlet4() { } protected void startApp() throws MIDletStateChangeException { dis=Display.getDisplay(this); dis.setCurrent(mgc); } protected void pauseApp() { } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } class MyGameCanvas extends GameCanvas implements Runnable{ private Image img; private Sprite sp; private Graphics gra; private boolean RUN=true; public MyGameCanvas(){ super(true); gra=this.getGraphics(); try{ img=Image.createImage("/donghua.png");//放入要切割的图片 sp=new Sprite(img,img.getWidth()/4,img.getHeight()); }catch(Exception ex){ ex.printStackTrace(); } new Thread(this).start(); } public void run(){ while(RUN){ try{ //清屏 gra.setColor(255,255,255); gra.fillRect(0, 0, this.getWidth(), this.getHeight()); sp.paint(gra); this.flushGraphics();//缓冲区画 sp.move(1, 1);//移动 sp.nextFrame();//动画效果 Thread.currentThread().sleep(100); }catch(Exception ex){ ex.printStackTrace(); } } } }}
------解决方案--------------------------------------------------------
- Java code
img = Image.createImage("/donghua.png"); // 放入要切割的图片sp = new Sprite(img, img.getWidth() / 4, img.getHeight());