命令有时不起作用

时间:2011-11-28 12:12:59

标签: java-me lcdui midp-2.0 cldc

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

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();
        }
    }
}

那么为什么Command有时不工作呢?使用阿尔卡特OT-806D手机测试应用程序

1 个答案:

答案 0 :(得分: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;
        //...
    }
    //...
}