在 Java J2ME 中加载图像
我在使用 java 2ME 加载图像时遇到问题。我在位置驱动器“C:”中有一个图像文件“picture.png”。之后我写了这样的内容来显示该位置的图像。
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class ImageMidlet extends MIDlet implements CommandListener{
private Display display;
private Command exitCommand;
private Command backCommand;
private Command okCommand;
private Form form;
private ImageItem imageItem;
private Image image;
public ImageMidlet(){
display = Display.getDisplay(this);
form=new Form("");
exitCommand = new Command("Exit", Command.EXIT, 1);
backCommand = new Command("Back", Command.BACK, 2);
okCommand = new Command("OK", Command.OK, 3);
try {
image=Image.createImage("/picture.png");
imageItem=new ImageItem(null,image,ImageItem.LAYOUT_NEWLINE_BEFORE,"");
}
catch(IOException ex){
}
form.append(imageItem);
form.addCommand(okCommand);
form.addCommand(exitCommand);
form.addCommand(backCommand);
form.setCommandListener(this);
display.setCurrent(form);
}
public void commandAction(Command c,Displayable d){
}
public void startApp() {
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
它向我显示了这个错误:
Unable to create MIDlet Test.ImageMidlet
java.lang.NullPointerException
at javax.microedition.lcdui.Form.append(Form.java:638)
at Test.ImageMidlet.<init>(ImageMidlet.java:39)
at java.lang.Class.runCustomCode(+0)
at com.sun.midp.midlet.MIDletState.createMIDlet(+34)
at com.sun.midp.midlet.Selector.run(Selector.java:151)
我正在开始学习开发,所以请指导如何做到这一点。
I have a problem with loading image with java 2ME. I have a image file "picture.png" in location drive "C:". After that I wrote my like this to show image from this location.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class ImageMidlet extends MIDlet implements CommandListener{
private Display display;
private Command exitCommand;
private Command backCommand;
private Command okCommand;
private Form form;
private ImageItem imageItem;
private Image image;
public ImageMidlet(){
display = Display.getDisplay(this);
form=new Form("");
exitCommand = new Command("Exit", Command.EXIT, 1);
backCommand = new Command("Back", Command.BACK, 2);
okCommand = new Command("OK", Command.OK, 3);
try {
image=Image.createImage("/picture.png");
imageItem=new ImageItem(null,image,ImageItem.LAYOUT_NEWLINE_BEFORE,"");
}
catch(IOException ex){
}
form.append(imageItem);
form.addCommand(okCommand);
form.addCommand(exitCommand);
form.addCommand(backCommand);
form.setCommandListener(this);
display.setCurrent(form);
}
public void commandAction(Command c,Displayable d){
}
public void startApp() {
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
It shows me this error:
Unable to create MIDlet Test.ImageMidlet
java.lang.NullPointerException
at javax.microedition.lcdui.Form.append(Form.java:638)
at Test.ImageMidlet.<init>(ImageMidlet.java:39)
at java.lang.Class.runCustomCode(+0)
at com.sun.midp.midlet.MIDletState.createMIDlet(+34)
at com.sun.midp.midlet.Selector.run(Selector.java:151)
I am starting learn to develop, so please guide to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Image.createImage(String name) 将给定图像加载为资源。资源通过 Class.getResourceAsStream(name) 加载,它从类路径查找资源,而不是从文件系统根目录查找资源。
您应该将图像文件放入类路径中,这通常是最终的应用程序 .jar 文件。通常会在项目下创建一个名为 resources 或 res 的文件夹,用于放置图像。然后将该文件夹的内容复制到 .jar 文件中。在开发阶段,您应该能够使用命令行参数(Java SE 中的 java -cp resources)或类似的 IDE 设置将资源文件夹附加到类路径。
如果您确实有兴趣从实际文件系统加载图像,则可以使用可选的 FileConnection API (http://developers.sun.com/mobility/apis/articles/fileconnection/)。不过,手机对该 API 的支持是有限的。对于静态图像,类路径是最佳选择。
Image.createImage(String name) loads the given image as a resource. Resources are loaded with Class.getResourceAsStream(name), which looks up the resources from classpath, not from your file system root.
You should put the image file in your classpath, which is usually the final application .jar file. Usually a folder called resources or res is created under the project, where the images are placed. The contents of this folder are then copied to the .jar file. In development phase you should be able to append your resource folder to the classpath with a command-line parameter (java -cp resources in Java SE) or with a similar IDE setting.
If you are really interested in loading the images from actual file system, you can use optional FileConnection API (http://developers.sun.com/mobility/apis/articles/fileconnection/). The handset support for this API is limited though. For static images the classpath is the way to go.
正如 msell 所说 - 您无法从计算机访问图像。确保您已将给定的图像包含在 midlet jar 文件中。如果您尝试使用“/picture.png”访问它,那么它应该位于 jar 的根目录中。
As msell said - You can't access images from Your computer. Make sure that You have included the given image in midlet jar file. If You try to access it using '/picture.png', then it should be located a the root directory in jar.
首先将您的图像放入默认包中。
我已将“网上邻居.png”放入默认包中。
然后创建名为“ImageItemExample”的 MIDlet
然后将以下代码复制到该 MIDlet 文件中。
First of all place your image in default package.
I have placed "My Network Places.png" in default package.
Then create MIDlet named "ImageItemExample"
then copy below code in that MIDlet file.
我的猜测是,这
会引发一个异常,该异常会阻止创建 ImageItem 类型的新对象,从而使 imageItem 变量保留为 null。这会给你带来空指针异常。
您的文件不是 Picture.png 而不是 Pictur.png 吗?
My guess is that
throws an exception which prevents the creation of a new object of type ImageItem which leaves your imageItem variable as null. This gives you the null pointer exception.
Isn't your file Picture.png and not Pictur.png?
验证文件 picture.png 是否确实存在,
具体取决于设备模拟器/IDE,应该有一种方法可以为设备设置“HOME”目录。在你的情况下,这将是“C:\”
Verify that the file picture.png actually exists
depending on the device emulator/IDE there should be a way to set the "HOME" directory for the device. In your case, this would be "C:\"