通过J2ME中的多个页面链接

问题描述

| 我已经学习J2ME已有几天了。但是我有一个大问题;我的移动应用程序大约有5页:第一页扩展了MIDlet,其他页面扩展了表单。加载第一页后,我使用命令切换到其他页面,但是当我进入新页面(扩展形式)时,我附加的所有命令都没有响应。我需要洞察力来解决这个问题。我的代码如下; 初始页;
        /*
     * 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\",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\",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\",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\",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\",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: \",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\",1);         
         spotted = new Command(\"Spotted\",4);
         sendReport = new Command(\"Send Report\",2);
         viewReport = new Command(\"Reports\",3);
         exit = new 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(){

    }


}
    

解决方法

        在您的订阅表单中,没有将CommandListener附加到订阅表单。因此,在Form上添加命令后,像attach2一样附加CommandListener。 谢谢