日期:2014-05-20  浏览次数:20717 次

j2me在加载图片时一直出现IO异常和空指针异常
我的代码如下:

import java.io.IOException;

//import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.TiledLayer;

public class TestCanvas1 extends GameCanvas implements Runnable{
   
private Graphics g;
private TiledLayer backLayer;
private Image backImage;  
private short PROJA_map1[][] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0 }
};
private LayerManager mLayerManager;
public TestCanvas1(){
super(true);
mLayerManager = new LayerManager();
g = getGraphics(); 
try {
backImage = Image.createImage("/res/testPicture.PNG");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

backLayer = new TiledLayer(22,500,backImage,16,16);
for (int i = 0; i < 500; i++) {
for (int j = 0; j < 22; j++) {
backLayer.setCell(j, i, PROJA_map1[i][j] + 1);
}
}
mLayerManager.append(backLayer);
  //backLayer.paint(g);
// flushGraphics();
}
public void start() {
Thread t = new Thread(this);
t.start();
}
public void run() {
// TODO Auto-generated method stub

int y = 6500;
g = getGraphics();
int w = this.getWidth();
int h = this.getHeight();

while (true) {
mLayerManager.setViewWindow(0, y, w, h);
y=y-30;
mLayerManager.paint(g, 0, 0);
// g.drawString("hello", this.getWidth() / 2, 0, Graphics.HCENTER
// | Graphics.TOP);
flushGraphics();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}



import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


/**
 * 在这个工程中对Game中图片的处理,进行试验、测试。
 * 这里是这个类的启动类。
 *
 */
public class testCanvas extends MIDlet implements CommandListener {
  private Command exit;
  private TestCanvas1 myGameCanvas;
public testCanvas() {
super();

exit = new Command("EXIT",Command.EXIT,1);
myGameCanvas = new TestCanvas1();
myGameCanvas.addCommand(exit);
myGameCanvas.setCommandListener(this);

myGameCanvas.start();
}

protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub