1

有一个Canvas有两个Commands。问题是,当第一次打开画布时,命令起作用,但是当我第二次打开它时,命令不起作用!这是代码:

package view;

import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.StringItem;


public class DetailPhotoClient extends Canvas implements CommandListener {

    private Command delete, back;
    private GaleriePhotos backForm;
    private FileConnection fcFile;
    private Image sourceImage;
    private InputStream is;
    private boolean ok,oom, io;

    public DetailPhotoClient(GaleriePhotos prevForm, String absolutePathphotoName)
    {
        super();

        back = new Command("Retour", Command.SCREEN, 1);
        addCommand(back);

        delete = new Command("Supprimer", Command.SCREEN, 2);
        addCommand(delete);

        setCommandListener(this);

        backForm = prevForm;

        ok = true;
        oom = false;
        io = false;

        try {
            fcFile = (FileConnection) Connector.open(absolutePathphotoName, Connector.READ);
            is = fcFile.openInputStream();
            sourceImage = Image.createImage(is);
            is.close();
            fcFile.close();
        } catch (IOException ex) {
            handleException();
        } catch (OutOfMemoryError oome) {
            handleOOM();
        }
    }
    private void handleException() {
        ok = false;
        io = true;
        repaint();
    }
    private void handleOOM() {
        ok = false;
        oom = true;
        repaint();
    }
    protected void paint(Graphics g) {

        StringItem chp;
        int chpW;
        int x, y = getHeight()/2;

        g.fillRect(0, 0, getWidth(), getHeight());

        if (ok)
            g.drawImage(sourceImage, 0, 0, Graphics.TOP | Graphics.LEFT);

        if (io)
        {
            chp = new StringItem(null,"Erreur média et/ou d'entrée-sortie !");
            chpW = chp.getPreferredWidth();
            x = ( getWidth() - chpW ) / 2 ;
            g.setColor(16711422);
            if (x<0)
                g.drawString("Erreur média et/ou d'entrée-sortie !", 0, y, Graphics.TOP | Graphics.LEFT);
            else
                g.drawString("Erreur média et/ou d'entrée-sortie !", x, y, Graphics.TOP | Graphics.LEFT);
        }

        if (oom)
        {
            chp = new StringItem(null,"Mémoire insuffisante !");
            chpW = chp.getPreferredWidth();
            x = ( getWidth() - chpW ) / 2 ;
            g.setColor(16711422);
            if (x<0)
                g.drawString("Mémoire insuffisante !", 0, y, Graphics.TOP | Graphics.LEFT);
            else
                g.drawString("Mémoire insuffisante !", x, y, Graphics.TOP | Graphics.LEFT);
        }
    }
    public void commandAction(Command c, Displayable d) {
        if (c == back)
            backForm.showBack();
        else
        {
            backForm.showBack();
            backForm.deletePhoto();
        }
    }
}

那么为什么命令有时不起作用呢?使用阿尔卡特 OT-806D 手机测试了该应用程序

4

1 回答 1

1

好吧,您的代码从一开始就广泛暴露于与 CommandListener 相关的威胁。看,您班级“外部”的任何代码都可能使您的命令反应迟钝:

void makeItDeaf(DetailPhotoClient detailPhotoClient) {
    detailPhotoClient.setCommandListener(null);
    // voila! your commands will be still visible but
    // wouldn't respond anymore
}

为确保您不会像上面那样意外破坏命令侦听器,请按如下方式“隐藏”它:

//...    
public class DetailPhotoClient extends Canvas { // no "implements" here

    private Command delete, back;
    private GaleriePhotos backForm;
    private FileConnection fcFile;
    private Image sourceImage;
    private InputStream is;
    private boolean ok,oom, io;

    public DetailPhotoClient(GaleriePhotos prevForm,
            String absolutePathphotoName)
    {
        super();

        back = new Command("Retour", Command.SCREEN, 1);
        addCommand(back);

        delete = new Command("Supprimer", Command.SCREEN, 2);
        addCommand(delete);

        backForm = prevForm;

        setCommandListener(new CommandListener() {
            public void commandAction(Command c, Displayable d) {
                if (backForm == null) {
                    System.out.println("backForm is null: ignore command");
                    return;
                }
                if (c == back) {
                    System.out.println("back command");
                    backForm.showBack();
                } else {
                    System.out.println("delete command");
                    backForm.showBack();
                    backForm.deletePhoto();
                }
            }
        });

        ok = true;
        oom = false;
        //...
    }
    //...
}
于 2011-11-29T08:03:33.947 回答