I have been learning J2ME for a couple of days now. but I have a major problem; My mobile application has about 5pages: the first page extends MIDlet while others extends form. After the first page is loaded, I used command to switch to other pages but when i get to the new page (which extended form) all commands i attached to is not responding. I need insight into solving this problem. My code is below;
Initial page;
/*
* 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[] ={"Traffi开发者_JAVA技巧c 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(){
}
*
*/
}
The second page; which tried to call the first page;
/*
* 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(){
}
}
Here in your Subscribe Form you not attached CommandListener to your Subscribe Form. So After adding commands on the Form, attach CommandListener like that this.setCommandListener(this);
Thanks
精彩评论