我正在为吉他店制作一个小型的j2me应用程序。 但我得到java.lang.IndexOutOfBoundsException错误。 我认为可变声明存在问题,但无法解决。 有人可以请帮帮我。
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
public class guitarStore extends MIDlet implements CommandListener
{
public int guitartype=0, guitarsize=0;
Form frm1;
Form frm2;
Form frm3;
Form frm4;
Form frm5;
Form frm7;
TextField tfUName;
TextField tfPswd;
TextField Amount;
String s1, s2, s3;
ChoiceGroup cgAccount;
ChoiceGroup cgTrans;
Command cmdOk; //frm1
Command cmdExit1;//frm1
Command cmdLogin;//frm2
Command cmdBack;//frm2
Command cmdNextTR;//frm3
Command cmdBackLG;//frm3
Command cmdBackAC;//frm4
Command cmdPAmount;//frm4
Command cmdBackTR;//frm5
Command cmdNote;//frm5
Command cmdExit2;//page7
Command cmdExit4;//page7
private Display display;
private Ticker frmTicker;
public guitarStore()
{
try
{
frm1 = new Form("Welcome");
frm2 = new Form("Member Login");
frm3 = new Form("Guitar Type");
frm4 = new Form("Guitar Style");
frm5 = new Form("Amount Page");
frm7 = new Form("Receipt");
display = Display.getDisplay(this);
frmTicker = new Ticker ("Furtados Guitar Store ");
frm1.setTicker(frmTicker);
cmdOk=new Command("NEXT",Command.SCREEN,1);//frm1
cmdExit1=new Command("EXIT",Command.EXIT,0);//frm1
tfUName=new TextField("Enter User Name : ","",30,TextField.ANY);
tfPswd=new TextField("Enter Password : ","",30,TextField.PASSWORD);
cmdBack=new Command("BACK",Command.BACK,0);//frm2
cmdLogin=new Command("Login",Command.SCREEN,1);//frm2
cgAccount=new ChoiceGroup("Guitar Style : ",Choice.EXCLUSIVE);
cgAccount.append("Acoustic Guitar",null);
cgAccount.append("Electric Guitar",null);
cgAccount.append("Bass Guitar",null);
cmdBackLG=new Command("BACK",Command.BACK,0);//frm3
cmdNextTR=new Command("NEXT",Command.SCREEN,1);//frm3
cgTrans=new ChoiceGroup("Size Selection : ",Choice.EXCLUSIVE);
cgTrans.append("Small ",null);
cgTrans.append("Medium",null);
cgTrans.append("Jambo",null);
cmdBackAC=new Command("BACK",Command.BACK,0);//frm4
cmdPAmount=new Command("NEXT",Command.SCREEN,1);//frm4
Amount=new TextField("Total Quantity : ","",10,TextField.ANY);
cmdBackTR=new Command("BACK",Command.BACK,0);//frm5
cmdNote=new Command("Next",Command.SCREEN,1);//frm5
cmdExit2=new Command("BACK",Command.BACK,0);//page7
cmdExit4=new Command("EXIT",Command.BACK,0);//page7
}
catch(Exception e)
{
System.out.println (e);
}
}
public void startApp()
{
frm1.addCommand(cmdOk);
frm1.addCommand(cmdExit1);
try
{
Image im = Image.createImage("/airplane.png");
frm1.append(new ImageItem(null, im, ImageItem.LAYOUT_CENTER , null));
display.setCurrent(frm1);
}
catch (java.io.IOException e)
{
System.err.println("Unable to locate or read the image file");
}
frm1.setCommandListener(this);
frm2.addCommand(cmdBack);
frm2.addCommand(cmdLogin);
frm2.append(tfUName);
frm2.append(tfPswd);
display=Display.getDisplay(this);
frm2.setCommandListener(this);
String msg = "Please select the Guitar type";
frm3.append(msg);
frm3.append(cgAccount);
frm3.addCommand(cmdBackLG);
frm3.addCommand(cmdNextTR);
frm3.setCommandListener(this);
display=Display.getDisplay(this);
frm4.append(cgTrans);
frm4.addCommand(cmdBackAC);
frm4.addCommand(cmdPAmount);
frm4.setCommandListener(this);
display=Display.getDisplay(this);
frm5.addCommand(cmdBackTR);
frm5.addCommand(cmdNote);
frm5.append(Amount);
display=Display.getDisplay(this);
frm5.setCommandListener(this);
frm7.addCommand(cmdExit2);
frm7.addCommand(cmdExit4);
display=Display.getDisplay(this);
frm7.setCommandListener(this);
}
public void pauseApp(){
}
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
public void commandAction(Command c,Displayable d)
{
try
{
if(c==cmdExit1)
{
destroyApp(false);
notifyDestroyed();
}
if(c==cmdOk)
{
display.setCurrent(frm2);
}
if(c==cmdLogin)
{
s2 = tfUName.getString();
s3 = tfPswd.getString();
if((s2.equals("admin"))&&(s3.equals("sumesh")))
{
display.setCurrent(frm3);
}
}
if(c==cmdBack)
{
display.setCurrent(frm1);
}
if(c==cmdNextTR)
{
boolean choice1;
choice1 = cgTrans.isSelected(0);
boolean choice2;
choice2 = cgTrans.isSelected(1);
boolean choice3;
choice3 = cgTrans.isSelected(3);
if(choice1==true)
{
guitartype=1;
}
if(choice2==true)
{
guitartype=2;
}
if(choice3==true)
{
guitartype=3;
}
display.setCurrent(frm4);
}
if(c==cmdBackLG)
{
guitartype=0;
display.setCurrent(frm2);
}
if(c==cmdPAmount)
{
boolean choice4;
choice4 = cgTrans.isSelected(0);
boolean choice5;
choice5 = cgTrans.isSelected(1);
boolean choice6;
choice6 = cgTrans.isSelected(3);
if(choice4==true)
{
guitarsize=3000;
}
if(choice5==true)
{
guitarsize=5000;
}
if(choice6==true)
{
guitarsize=7000;
}
display.setCurrent(frm5);
}
if(c==cmdBackLG)
{
guitarsize=0;
display.setCurrent(frm3);
}
if(c==cmdNote)
{
s1 = Amount.getString();
int s4 = Integer.parseInt(s1);
if(s4>10)
{
Alert bal1 = new Alert("Transaction not allowed","Not more than 10 guitars can be purchased at a time", null, AlertType.INFO);
bal1.setTimeout(5000);
display.setCurrent(bal1,frm7);
}
else
{
int prize,totalprize;
prize=s4*(guitartype*guitarsize);
totalprize=prize;
frm7.append("Total cost of purchase is Rs: " + prize + "");
display.setCurrent(frm7);
}
}
if(c==cmdBackTR)
{
display.setCurrent(frm4);
}
if(c==cmdExit2)
{
display.setCurrent(frm4);
}
if(c==cmdExit4)
{
destroyApp(false);
notifyDestroyed();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
答案 0 :(得分:3)
您正在尝试检查第4个选定的索引(在2个位置):
choice3 = cgTrans.isSelected(3);
但仅使用3个项目填充了您的ChoiceGroup
窗口小部件。更好用:
choice3 = cgTrans.isSelected(2);