I have read in the book that getDisplay() can only be called after the beginning of the MIDlet's startApp() method.
Although I have called getDisplay() in the startApp() , why I cannot call getDisplay as follows.
Because I cannot call at that place, I have to write getDisplay() whenever I want to show an alert.
How to solve that problem?
Code:package treadstringrecord; 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.midlet.MIDletStateChangeException; import javax.microedition.rms.RecordStore; import javax.microedition.rms.RecordStoreException; import javax.microedition.rms.RecordStoreNotOpenException; public class Mainformcommandhandler implements CommandListener { ////////////////Why I could not call getDisplay here/////////////////////////////////////// //Display display=Display.getDisplay(TReadStringRecordMIDlet.treadstringrecordmidlet); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Display display; RecordStore rs; Alert alert; public Mainformcommandhandler() { // TODO Auto-generated constructor stub } public void commandAction(Command c, Displayable d) { MainForm mainform=(MainForm)d; if(c==mainform.cmdExit) { try { TReadStringRecordMIDlet.treadstringrecordmidlet.destroyApp(true); } catch (MIDletStateChangeException e) { // TODO Auto-generated catch block e.printStackTrace();/////////////////////////////////see how that is written in the book } } if(c==mainform.cmdOpen) { try { rs=RecordStore.openRecordStore("comment", true); } catch (RecordStoreException e) { // TODO Auto-generated catch block //e.printStackTrace(); alert=new Alert("Cannot open recordstore",e.toString(),null,AlertType.ERROR); display=Display.getDisplay(TReadStringRecordMIDlet.treadstringrecordmidlet); display.setCurrent(alert); } String comment1="Excellent"; String comment2="very nice"; //byte[] readbyte=null; byte[] commentbyte =comment1.getBytes(); byte[] commentbyte2=comment2.getBytes(); try { rs.addRecord(commentbyte, 0, commentbyte.length); rs.addRecord(commentbyte2, 0, commentbyte2.length); } catch (RecordStoreException e) { // TODO Auto-generated catch block alert=new Alert("Cannot write recordstore",e.toString(),null,AlertType.ERROR); display=Display.getDisplay(TReadStringRecordMIDlet.treadstringrecordmidlet); display.setCurrent(alert); } try { StringBuffer buffer=new StringBuffer(); for(int i=1;i<=rs.getNumRecords();i++) { String readrecord=new String(rs.getRecord(i)); buffer.append(readrecord); } alert=new Alert("Reading recordstore",buffer.toString(),null,AlertType.INFO); display=Display.getDisplay(TReadStringRecordMIDlet.treadstringrecordmidlet); display.setCurrent(alert); alert.setTimeout(Alert.FOREVER); } catch (RecordStoreNotOpenException e) { alert=new Alert("There is no recordstore",e.toString(),null,AlertType.ERROR); display=Display.getDisplay(TReadStringRecordMIDlet.treadstringrecordmidlet); display.setCurrent(alert); } catch (RecordStoreException e) { alert=new Alert("error in reading recordstore",e.toString(),null,AlertType.ERROR); display=Display.getDisplay(TReadStringRecordMIDlet.treadstringrecordmidlet); display.setCurrent(alert); alert.setTimeout(Alert.FOREVER); } try { rs.closeRecordStore(); } catch (RecordStoreException e) { e.printStackTrace(); } }//if (c==mainform.cmdOpen) if(c==mainform.cmdDelete) { try { rs=RecordStore.openRecordStore("comment", false); try { int i=rs.getNumRecords(); while(i!=0) { rs.deleteRecord(i); i--; } } catch (RecordStoreNotOpenException e) { e.printStackTrace(); } catch (RecordStoreException e) { e.printStackTrace(); } } catch (RecordStoreException e) { alert=new Alert("Cannot open recordstore",e.toString(),null,AlertType.ERROR); display=Display.getDisplay(TReadStringRecordMIDlet.treadstringrecordmidlet); display.setCurrent(alert); } }//if(c==mainform.cmdDelete) } }

Reply With Quote





