在写代码的时候,出现一个错误,我的错误地方为boolean choice[]=new boolean[list.size];下次注意。
package mtk; import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.List; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; public class ListCheckBox extends MIDlet implements CommandListener { private Display display; private final static Command CMD_EXIT=new Command("退出",Command.EXIT,1); private Command SUBMIT=new Command("提交",Command.SCREEN,1); private List list; public ListCheckBox() { display=Display.getDisplay(this); list=new List("媒体选择",List.MULTIPLE); list.append("书籍", null); list.append("电影", null); list.append("电视剧", null); list.append("广播", null); list.addCommand(CMD_EXIT); list.addCommand(SUBMIT); list.setCommandListener(this); } protected void destroyApp(boolean arg0) { } protected void pauseApp() { } protected void startApp() throws MIDletStateChangeException { display.setCurrent(list); } public void commandAction(Command c, Displayable d) { if(c==CMD_EXIT){ destroyApp(false); notifyDestroyed(); }else{ if(c==SUBMIT){ boolean choice[] = new boolean[list.size()]; StringBuffer message=new StringBuffer(); list.getSelectedFlags(choice); for(int x=0;x<choice.length;x++){ if(choice[x]){ message.append(list.getString(x)); message.append(" "); } } Alert alert=new Alert("选项",message.toString(),null,null); alert.setTimeout(Alert.FOREVER); alert.setType(AlertType.INFO); display.setCurrent(alert); list.removeCommand(SUBMIT); } } } }