Commands sometimes don't work

余生长醉 提交于 2019-12-11 07:21:28

问题


There is a Canvas which has two Commands. The problem is that when the canvas is first opened then the commands work , but when I open it for the second time then the command doesn't work! Here is the code:

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

So why doesn't the Command work sometimes? Tested the app with an Alcatel OT-806D phone


回答1:


Well your code is widely exposed to threats related to CommandListener to start with. Look, any piece of code "outside" your class can make your commands irresponsive:

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

To make sure that you don't accidentally break the command listener like above, "hide" it about as follows:

//...    
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;
        //...
    }
    //...
}


来源:https://stackoverflow.com/questions/8295639/commands-sometimes-dont-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!