2011-06-12 5 views
1

저는 2,3 일 동안 J2ME를 배웠습니다. 하지만 큰 문제가 있습니다. 내 모바일 애플리케이션에는 약 5 페이지가 있습니다. 첫 번째 페이지는 MIDlet을 확장하고 다른 페이지는 양식을 확장합니다. 첫 번째 페이지가로드 된 후 다른 페이지로 전환하는 명령을 사용했으나 새 페이지 (확장 된 양식)에 연결하면 연결된 모든 명령이 응답하지 않습니다. 이 문제를 해결하려면 통찰력이 필요합니다. 내 코드는 아래와 같습니다.J2ME에서 여러 페이지로 연결

초기 페이지;

 /* 
    * To change this template, choose Tools | Templates 
    * and open the template in the editor. 
    */ 
    package report; 

    import javax.microedition.midlet.*; 
    import javax.microedition.lcdui.*; 
    import org.netbeans.microedition.lcdui.SimpleTableModel; 
    import org.netbeans.microedition.lcdui.TableItem; 

    /** 
    * @author Possicon 
    */ 
    public class Reports extends MIDlet implements CommandListener { 

     private boolean midletPaused = false; 

     //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">      
     private Form form; 
     private Command SendReport; 
     private Command SendPictureReport; 
     private Command exitCommand; 
     private Command viewReport; 
     private Command subscribe; 
     private Command spotted; 
     private Command sortBy; 
     private SimpleTableModel tableModel1; 
     //</editor-fold>      
     /** 
     * The Reports constructor. 
     */ 
     public Reports() { 
      //startMIDlet(); 
     } 

     //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">      
     //</editor-fold>      
     //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">           
     /** 
     * Initializes the application. 
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method. 
     */ 
     private void initialize() {           
      // write pre-initialize user code here 

      // write post-initialize user code here 
     }        
     //</editor-fold>       

     //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">           
     /** 
     * Performs an action assigned to the Mobile Device - MIDlet Started point. 
     */ 
     public void startMIDlet() {          
      // write pre-action user code here 

       getDisplay().setCurrent(getForm()); 


      // write post-action user code here 
     }        
     //</editor-fold>       

     //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">           
     /** 
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point. 
     */ 
     public void resumeMIDlet() {          
      // write pre-action user code here 

      // write post-action user code here 
     }        
     //</editor-fold>        

     //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">            
     /** 
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable. 
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately 
     * @param nextDisplayable the Displayable to be set 
     */ 
     public void switchDisplayable(Alert alert, Displayable nextDisplayable) {            
      // write pre-switch user code here 
      Display display = getDisplay();            
      if (alert == null) { 
       display.setCurrent(nextDisplayable); 
      } else { 
       display.setCurrent(alert, nextDisplayable); 
      }            
      // write post-switch user code here 
     }         
     //</editor-fold>         

     //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">             
     /** 
     * Called by a system to indicated that a command has been invoked on a particular displayable. 
     * @param command the Command that was invoked 
     * @param displayable the Displayable where the command was invoked 
     */ 
     public void commandAction(Command command, Displayable displayable) {            
      // write pre-action user code here 
      if (displayable == form) {           
       if (command == SendReport) {           
        // write pre-action user code here 
        getDisplay().setCurrent(new SendReport()); 

        // write post-action user code here 
       } else if (command == exitCommand) {           
        // write pre-action user code here 
        exitMIDlet(); 

        // write post-action user code here 
       } else if (command == sortBy) {           
        // write pre-action user code here 

        // write post-action user code here 
       } else if (command == spotted) {           
        // write pre-action user code here 
        getDisplay().setCurrent(new Spotted()); 

        // write post-action user code here 
       } else if (command == subscribe) {           
        // write pre-action user code here 
        //getDisplay().setCurrent(new Subscribe()); 
        new Subscribe().getMyDisplay(getDisplay()); 

        // write post-action user code here 
       }             
      }             
      // write post-action user code here 
     }            
     //</editor-fold>           


     //<editor-fold defaultstate="collapsed" desc=" Generated Getter: form ">         
     /** 
     * Returns an initiliazed instance of form component. 
     * @return the initialized component instance 
     */ 
     public Form getForm() { 
      if (form == null) {         
       // write pre-init user code here 
       form = new Form("Traffic Reports", new Item[] { });          
       form.addCommand(getSortBy()); 
       form.addCommand(getSendReport()); 
       form.addCommand(getSubscribe()); 
       form.addCommand(getExitCommand()); 
       form.addCommand(getSpotted()); 
       form.setCommandListener(this);         
       // write post-init user code here 
       loadReports(form); 
       //form.append(getTableModel1()); 
      }       
      return form; 
     } 
     //</editor-fold>      



     //<editor-fold defaultstate="collapsed" desc=" Generated Getter: SendReport ">         
     /** 
     * Returns an initiliazed instance of SendReport component. 
     * @return the initialized component instance 
     */ 
     public Command getSendReport() { 
      if (SendReport == null) {         
       // write pre-init user code here 
       SendReport = new Command("Send Report", Command.SCREEN, 0);         
       // write post-init user code here 
      }       
      return SendReport; 
     } 
     //</editor-fold>      

     //<editor-fold defaultstate="collapsed" desc=" Generated Getter: SendPictureReport ">         
     /** 
     * Returns an initiliazed instance of SendPictureReport component. 
     * @return the initialized component instance 
     */ 
     public Command getSendPictureReport() { 
      if (SendPictureReport == null) {         
       // write pre-init user code here 
       SendPictureReport = new Command("Send Picture Report", Command.SCREEN, 0);         
       // write post-init user code here 
      }       
      return SendPictureReport; 
     } 
     //</editor-fold>      

     //<editor-fold defaultstate="collapsed" desc=" Generated Getter: tableModel1 ">         
     /** 
     * Returns an initiliazed instance of tableModel1 component. 
     * @return the initialized component instance 
     */ 
     public SimpleTableModel getTableModel1() { 
      if (tableModel1 == null) {         
       // write pre-init user code here 
       tableModel1 = new SimpleTableModel(new java.lang.String[][] {          
        new java.lang.String[] { "Traffic on Ikorodu road, free" }, 
        new java.lang.String[] { "Adeola Odeku is free" }, 
        new java.lang.String[] { "Accident on Ahmadu Bellow" }}, null);         
       // write post-init user code here 
      }       
      return tableModel1; 
     } 
     //</editor-fold>      

     //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">         
     /** 
     * Returns an initiliazed instance of exitCommand component. 
     * @return the initialized component instance 
     */ 
     public Command getExitCommand() { 
      if (exitCommand == null) {         
       // write pre-init user code here 
       exitCommand = new Command("Exit", Command.EXIT, 0);         
       // write post-init user code here 
      }       
      return exitCommand; 
     } 
     //</editor-fold>      

     //<editor-fold defaultstate="collapsed" desc=" Generated Getter: viewReport ">         
     /** 
     * Returns an initiliazed instance of viewReport component. 
     * @return the initialized component instance 
     */ 
     public Command getViewReport() { 
      if (viewReport == null) {         
       // write pre-init user code here 
       viewReport = new Command("View Reports", Command.SCREEN, 0);         
       // write post-init user code here 
      }       
      return viewReport; 
     } 
     //</editor-fold>      

     //<editor-fold defaultstate="collapsed" desc=" Generated Getter: subscribe ">         
     /** 
     * Returns an initiliazed instance of subscribe component. 
     * @return the initialized component instance 
     */ 
     public Command getSubscribe() { 
      if (subscribe == null) {         
       // write pre-init user code here 
       subscribe = new Command("Subscribe", Command.SCREEN, 0);         
       // write post-init user code here 
      }       
      return subscribe; 
     } 
     //</editor-fold>      

     //<editor-fold defaultstate="collapsed" desc=" Generated Getter: spotted ">         
     /** 
     * Returns an initiliazed instance of spotted component. 
     * @return the initialized component instance 
     */ 
     public Command getSpotted() { 
      if (spotted == null) {         
       // write pre-init user code here 
       spotted = new Command("Spotted", Command.SCREEN, 0);         
       // write post-init user code here 
      }       
      return spotted; 
     } 
     //</editor-fold>      

     //<editor-fold defaultstate="collapsed" desc=" Generated Getter: sortBy ">         
     /** 
     * Returns an initiliazed instance of sortBy component. 
     * @return the initialized component instance 
     */ 
     public Command getSortBy() { 
      if (sortBy == null) {         
       // write pre-init user code here 
       sortBy = new Command("Sort By", Command.SCREEN, 0);         
       // write post-init user code here 
      }       
      return sortBy; 
     } 
     //</editor-fold>      



     /** 
     * Returns a display instance. 
     * @return the display instance. 
     */ 
     public Display getDisplay() { 
      return Display.getDisplay(this); 
     } 

     /** 
     * Exits MIDlet. 
     */ 
     public void exitMIDlet() { 
      switchDisplayable(null, null); 
      destroyApp(true); 
      notifyDestroyed(); 
     } 

     /** 
     * Called when MIDlet is started. 
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet. 
     */ 
     public void startApp() { 
      if (midletPaused) { 
       resumeMIDlet(); 
      } else { 
       initialize(); 
       //Login displayed = Login.displayed(1); 
       //getDisplay().setCurrent(new Login()); 
       startMIDlet(); 

      } 
      midletPaused = false; 
     } 

     /** 
     * Called when MIDlet is paused. 
     */ 
     public void pauseApp() { 
      midletPaused = true; 
     } 

     /** 
     * Called to signal the MIDlet to terminate. 
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released. 
     */ 
     public void destroyApp(boolean unconditional) { 
     } 

     public void loadReports(Form form){ 
      String string[] ={"Traffic is here", "Traffic is bad here","Accident Occured here"}; 
      StringItem st; 
      for(int i=0; i<string.length; i++){ 
       st = new StringItem("",string[i]); 
       form.append(st); 
      } 
     } 

     /* 
     public Array fetchReports(){ 

     } 
     * 
     */ 

} 

두 번째 페이지; 첫 번째 페이지를 호출하려고 시도했습니다.

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package report; 

import javax.microedition.lcdui.Command; 
import javax.microedition.lcdui.CommandListener; 
import javax.microedition.lcdui.DateField; 
import javax.microedition.lcdui.Display; 
import javax.microedition.lcdui.Displayable; 
import javax.microedition.lcdui.Form; 
import javax.microedition.lcdui.Item; 
import javax.microedition.lcdui.ItemStateListener; 
import javax.microedition.lcdui.StringItem; 
import javax.microedition.lcdui.TextField; 

/** 
* @author Possicon 
*/ 
public class Subscribe extends Form implements CommandListener, ItemStateListener { 

    private Form form; 
    private StringItem label; 
    private Display display; 
    private Command save; 
    private Command subscribe; 
    private Command spotted; 
    private Command sendReport ; 
    private Command viewReport; 
    private Command exit; 
    private Command back ; 
    private TextField email, phone,route; 
    private DateField time , timet; 

    /* 
    public Subscribe(){ 
     super(""); 
    } 
    * 
    */ 

    public Subscribe() { 
     //display = Display.getDisplay(this); 
     //display = displ; 
     //form = new Form("Traffic Subscription"); 
     super("Traffic Subscription"); 
     label = new StringItem("Subscribe to recieve Traffic Alerts: ", ""); 
     email = new TextField("Email: ", "", 50, TextField.EMAILADDR); 
     phone = new TextField("Phone: ", "", 11, TextField.ANY); 
     route = new TextField("Road: ", "", 20, TextField.ANY); 
     //twitter = new TextField("Twitter: ", "", 20, TextField.); 
     time = new DateField("Between: ", DateField.TIME); 
     timet = new DateField("To: ", DateField.TIME); 
     //DateFielddateField= new DateField(“Date:”, DateField.DATE); 
     java.util.Date date = new java.util.Date(); 
     time.setDate(date); 

     append(label); 
     append(email); 
     append(phone); 
     append(route); 
     append(time); 
     append(timet); 

     save = new Command("Save", Command.SCREEN,1);   
     spotted = new Command("Spotted", Command.SCREEN,4); 
     sendReport = new Command("Send Report", Command.SCREEN,2); 
     viewReport = new Command("Reports", Command.SCREEN,3); 
     exit = new Command("Exit", Command.EXIT,0); 
     //back = new Command("Back", Command.BACK,0); 

     addCommand(exit); 
     addCommand(viewReport); 
     addCommand(sendReport);    
     addCommand(save); 

     addCommand(spotted); 

     //return form; 
    } 


    public void commandAction(Command command, Displayable dsplbl) { 

     if (command == sendReport) { 
       // write pre-action user code here 
       new Reports().switchDisplayable(null,new SendReport()); 
       //display.setCurrent(new SendReport()); 

       // write post-action user code here 
      } else if (command == exit) { 
       // write pre-action user code here 
       //exitMIDlet(); 

       // write post-action user code here 
      } else if (command == spotted) { 
       // write pre-action user code here 
       display.setCurrent(new Spotted()); 

       // write post-action user code here 
      } 
     } 


    public void itemStateChanged(Item item) { 

    } 

    public void save(){ 

    } 


} 

답변

0

여기 귀하의 가입 양식에는 귀하의 가입 양식에 CommandListener가 첨부되지 않았습니다. Form에 명령을 추가 한 후 CommandListener를 붙이십시오. this.setCommandListener(this); 감사합니다.

+0

그래, 이제는 클릭에 응답하지만 오류가 발생합니다. Spotted is nullpointer error를 클릭합니다. 발견 된 페이지가 양식을 확장합니다. 즉, 다른 양식의 명령에서 새 양식으로 전환 할 수 없습니까? 도와 줘서 고마워. –

관련 문제