-1

サーバーから J2ME 電話にイメージをダウンロードするコードで、以下の 2 つのエラーが発生します。それらを削除する方法を教えてください。

java.lang.NullPointerException: 0

ServerImages.ImageMidletServlet$Test.run(ImageMidletServlet.java:102) で

java.lang.Thread.run() で、bci=11

キャッチされない例外: java.lang.IllegalArgumentException

javax.microedition.lcdui.Display.setCurrent()、bci=160

  • ServerImages.ImageMidletServlet$Test.showAlert(ImageMidletServlet.java:117) で

ServerImages.ImageMidletServlet$Test.run(ImageMidletServlet.java:108) で

java.lang.Thread.run() で、bci=11

使用したコードは以下

public class ImageMidletServlet extends MIDlet implements CommandListener {

    Display display = null;
    Form form = null;
    String url = "http://localhost:8080/C:/Users/HASENDE/Pictures/ase.jpg";
    Command backCommand = new Command("Back", Command.BACK, 0);
    Command submitCommand = new Command("Submit", Command.OK, 2);
    Command exitCommand = new Command("Exit", Command.STOP, 3);
    private Test test;

    public ImageMidletServlet() {}

    public void startApp() throws MIDletStateChangeException {
        display = Display.getDisplay(this);
        form = new Form("Show Image");
        form.addCommand(submitCommand);
        form.addCommand(exitCommand);
        form.setCommandListener(this);
        display.setCurrent(form);
    }

    public void pauseApp() {}

    public void destroyApp(boolean unconditional) {}

    public void commandAction(Command c, Displayable d) {
        if (c == exitCommand) {
            destroyApp(true);
            notifyDestroyed();
        } else if (c == backCommand) {
            display.setCurrent(form);
        } else if (c == submitCommand) {
            test = new Test(this);
            test.start();
        }
    }

    class Test implements Runnable {

        ImageMidletServlet midlet;
        private Display display;

        public Test(ImageMidletServlet midlet) {
            this.midlet = midlet;
            display = Display.getDisplay(midlet);
        }

        public void start() {
            Thread t = new Thread(this);
            t.start();
        }

        public void run() {
            DataInputStream is = null;
            StringBuffer sb = new StringBuffer();
            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");
                    ImageItem imgItem = new ImageItem("", img,
                            ImageItem.LAYOUT_NEWLINE_AFTER
                                | ImageItem.LAYOUT_CENTER, null);
                    f.append(imgItem);
                    display.setCurrent(f);
                } else {
                    showAlert("length is null");
                    ;
                }
                is.close();
                c.close();
            } catch (Exception e) {
                e.printStackTrace();
                showAlert(e.getMessage());
            }
        }

        /* Display Error On screen */
        private void showAlert(String err) {
            Alert a = new Alert("");
            a.setString(err);
            a.setTimeout(Alert.FOREVER);
            display.setCurrent(a);
        }
    };
}

また、コマンド送信を実行すると、エミュレーターが画像をダウンロードする代わりに「長さがnull」という選択肢を表示する理由がわかりません。どんな助けでも大歓迎です!

4

1 に答える 1

0

一部のサーバーでは、HTTP ヘッダーの Content-Length が正しく設定されていません。これは、読み取るコンテンツがある場合でも c.getLength() が 0 (ゼロ) を返す可能性があるためです。

バッファを使用する代わりに、Connector.open の直後に Image.createImage(c.openDataInputStream()) を試すことができます。

于 2012-04-09T21:09:57.367 に答える