JavaME

Fermé
rajun87 Messages postés 2 Date d'inscription jeudi 4 juin 2015 Statut Membre Dernière intervention 4 juin 2015 - 4 juin 2015 à 08:27
rajun87 Messages postés 2 Date d'inscription jeudi 4 juin 2015 Statut Membre Dernière intervention 4 juin 2015 - 4 juin 2015 à 08:29
Bonjour,
je suis encore novice en JavaME, c'est pourquoi j'ai réalisé un petit calculette,mais cela ne fonctionne pas alors qu'il n'y a pas d'erreur sur le code. c'est sur l'émulateur qu'il y a un problème, il me dit:" Application start failed".
Voici mon code:

package calculatricerajunjavame;

import java.io.IOException;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
/**
*
  • @author rajun
  • /

public class CalculatriceRajunJavaME extends MIDlet implements CommandListener {

private Form frmMain;
private Image imgAlert;
private TextField txfNumber1;
private TextField txfNumber2;
private StringItem stritemResult;
private ChoiceGroup chgOperation;
private Alert mAlert;
private Command cmdExit;
private Command cmdCalculate;
private final String strTitle = "Calculator";
private final String strNumber1 = "1st number:";
private final String strNumber2 = "2nd number:";
private final String strOperation = "Operation:";
private final String strResult = "Result:";

private final String strPathAlert = "/image/alert.png";
private final String strExit = "Exit";
private final String strCalculate = "Calculate";
private final String strTitleAlert = "Message";
private final String strMessageConvNumb = "The number is incorrect.";
private final String strMessageDivByZero = "It is not possible the division by zero.";
private final String[] strOperationName = new String[]
{ "Addition", "Subtraction", "Multiplication", "Division" };
private final String[] strPathImage = new String[]
{ "/image/plus.png", "/image/moins.png",
"/image/multi.png", "/image/div.png" };
private final Image[] imgOperationImage = new Image[4];

// Declaration of attributs for the calcule
private double dNumber1;
private double dNumber2;
private double dResult;
private int iOperation;

public void startApp() {
initComponents();
}

public void pauseApp() {

}

public void destroyApp(boolean unconditional) {
notifyDestroyed();
}

public void initComponents() {
frmMain = new Form(strTitle);
txfNumber1 = new TextField(strNumber1,"",10,TextField.ANY);
txfNumber2 = new TextField(strNumber2,"",10,TextField.ANY);
try {
for(int i = 0; i < strPathImage.length; i++) {
imgOperationImage[i] = Image.createImage(strPathImage[i]);
}
chgOperation = new ChoiceGroup (strOperation, Choice.EXCLUSIVE,
strOperationName, imgOperationImage);
} catch(IOException ioe) {
chgOperation = new ChoiceGroup (strOperation, Choice.EXCLUSIVE,
strOperationName, null);
}

stritemResult = new StringItem(strResult,"");
cmdExit = new Command(strExit, Command.EXIT, 1);
cmdCalculate = new Command(strCalculate, Command.OK, 1);
frmMain.append(txfNumber1);
frmMain.append(chgOperation);
frmMain.append(txfNumber2);
frmMain.append(stritemResult);

frmMain.addCommand(cmdExit);
frmMain.addCommand(cmdCalculate);
frmMain.setCommandListener(this);
Display.getDisplay(this).setCurrent(frmMain);
}
public void commandAction(Command c, Displayable d) {
if(c == cmdCalculate) {
if(verifyForm()) {
calculate();
show();
} else {
showAlert(strTitleAlert, strMessageConvNumb);
}
} else if(c == cmdExit) {
Display.getDisplay(this).setCurrent(null);
this.destroyApp(true);
this.notifyDestroyed();
}
}

public boolean verifyForm() {
try {
dNumber1 = Double.parseDouble(txfNumber1.getString());
dNumber2 = Double.parseDouble(txfNumber2.getString());
String strValue = chgOperation.getString(chgOperation.getSelectedIndex());
if(strValue.equals(strOperationName[0])) {
iOperation = 0;
} else if(strValue.equals(strOperationName[1])) {
iOperation = 1;
} else if(strValue.equals(strOperationName[2])) {
iOperation = 2;
} else {
iOperation = 3;
}
} catch(Exception e) {
return false;
}
return true;
}

public void calculate() {
switch(iOperation) {
case 0:
dResult = dNumber1 + dNumber2;
break;
case 1:
dResult = dNumber1 - dNumber2;
break;
case 2:
dResult = dNumber1 * dNumber2;
break;
case 3:
if(dNumber2 != 0) {
dResult = dNumber1 / dNumber2;
} else {
showAlert(strTitleAlert, strMessageDivByZero);
}
}
}

public void show() {
stritemResult.setText("" + dResult);
}

public void showAlert(String strTitleAlert, String strMessageAlert) {
try {
imgAlert = Image.createImage(strPathAlert);
//mAlert = new Alert(strTitleAlert,strMessageAlert,AlertType.WARNING);
mAlert.setTimeout(2000);
Display.getDisplay(this).setCurrent(mAlert);
} catch(Exception e1) {
}
}


}

1 réponse

rajun87 Messages postés 2 Date d'inscription jeudi 4 juin 2015 Statut Membre Dernière intervention 4 juin 2015
4 juin 2015 à 08:29
un petit coup de main serait la bienvenue.merci
0