Java me Graphics.drawImage NullPointerException与良好图像J2ME
g、 drawImageimg,0,0抛出NullPointerException。这意味着img是空的。我可以将img与ImageItem一起使用,并且它不为null。问题是什么?在调用g.drawImageimg,0,0之前,您正在设置图形g=null。不是你的img是空的。它是您的图形对象 在调用gc.paintg(另一个NullPointerException)之前,还需要设置GameCanvas gc=null 3行 最重要的是,你不能用这种方法在游戏画布上创建和绘制。GameCanvas是一个抽象类。所以你需要创建你自己的类来扩展GameCanvas——如果你想要一个GameCanvas的话。我也不确定情况是否如此,因为如果您只想显示一张图片,您可以很容易地满足于表单 请尝试以下方法:Java me Graphics.drawImage NullPointerException与良好图像J2ME,java-me,Java Me,g、 drawImageimg,0,0抛出NullPointerException。这意味着img是空的。我可以将img与ImageItem一起使用,并且它不为null。问题是什么?在调用g.drawImageimg,0,0之前,您正在设置图形g=null。不是你的img是空的。它是您的图形对象 在调用gc.paintg(另一个NullPointerException)之前,还需要设置GameCanvas gc=null 3行 最重要的是,你不能用这种方法在游戏画布上创建和绘制。GameCanv
Display display = null;
String url = "http://188.2.222.253/screenshot.png";
DataInputStream is = null;
Image img= null;
try
{
HttpConnection c = (HttpConnection) Connector.open(url);
int len = (int)c.getLength();
if (len > 0)
{
is = c.openDataInputStream();
byte[] data = new byte[len];
is.readFully(data);
img = Image.createImage(data, 0, len);
Form f = new Form("Image");
GameCanvas gc = null;
Graphics g = null;
g.drawImage(img, 0, 0, 0); //NullPointerException
gc.paint(g);
display.setCurrent(gc);
}
else
{
showAlert("length is null");
}
is.close();
c.close();
}
catch (Exception e)
{
e.printStackTrace();
showAlert(e.getMessage());
}
你能发布可以工作的代码吗?我不知道如何声明g和如何生成类。我想使用GameCanvas全屏显示图像。
img = Image.createImage(data, 0, len);
Form f = new Form("Image");
f.append(img);
display.setCurrent(f);