4

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)

私は開発を学び始めているので、これを行うためのガイドをお願いします。

4

5 に答える 5

6

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.

于 2009-08-26T05:16:35.130 に答える
4

msellが言ったように- あなたのコンピュータから画像にアクセスすることはできません。指定されたイメージが midlet jar ファイルに含まれていることを確認してください。「/picture.png」を使用してアクセスしようとすると、jar のルート ディレクトリにある必要があります。

于 2009-08-26T06:50:53.083 に答える
2

まず、イメージをデフォルトのパッケージに配置します。デフォルトのパッケージに「My Network Places.png」を配置しました。次に、「ImageItemExample」という名前の MIDlet を作成し、その MIDlet ファイルに以下のコードをコピーします。

import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class ImageItemExample extends MIDlet implements CommandListener{
private Display display;
private Command exit;
private Form form;
private ImageItem logo;

public ImageItemExample(){
form = new Form("Image Item");
exit = new Command("Exit", Command.EXIT, 0);
try{
  logo = new ImageItem(null, Image.createImage("/My Network Places.png"),
   ImageItem.LAYOUT_CENTER | ImageItem.LAYOUT_NEWLINE_BEFORE |
    ImageItem.LAYOUT_NEWLINE_AFTER, "Roseindia");
  form.append(logo);
}catch(IOException e){
  form.append(new StringItem(null, "Roseindia: Image not available: "+ e));
}
}

public void startApp(){
display = Display.getDisplay(this);
form.addCommand(exit);
form.setCommandListener(this);
display.setCurrent(form);
}

public void pauseApp(){}

public void destroyApp(boolean unconditional){
notifyDestroyed();
}

public void commandAction(Command c, Displayable d){
String label = c.getLabel();
if(label.equals("Exit")){
  destroyApp(true);
}
}
}
于 2010-10-06T16:33:05.290 に答える
-2

私の推測では

image=Image.createImage("/picture.png");

imageItem変数をnullのままにするImageItemタイプの新しいオブジェクトの作成を妨げる例外をスローします。これにより、nullポインタ例外が発生します。

あなたのファイルはPictur.pngではなくPicture.pngではありませんか?

于 2009-08-26T03:47:10.773 に答える
-2

ファイルpicture.pngが実際に存在することを確認します

デバイス エミュレータ/IDE によっては、デバイスの "HOME" ディレクトリを設定する方法があるはずです。あなたの場合、これは「C:\」になります

于 2009-08-26T05:11:39.540 に答える