命令有时不起作用 [英] Commands sometimes don't work

查看:98
本文介绍了命令有时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个Canvas,其中有两个Command.问题在于,当第一次打开画布时,该命令起作用,但是当我第二次打开画布时,该命令不起作用!这是代码:

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

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

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

推荐答案

首先,您的代码被广泛暴露于与CommandListener相关的威胁.看,类外"的任何一段代码都会使您的命令无响应:

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

这篇关于命令有时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆