在 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", 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(){
}
}
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[] ={"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(){
}
*
*/
}
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(){
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的订阅表单中,您没有将 CommandListener 附加到您的订阅表单。因此,在表单上添加命令后,请像
this.setCommandListener(this);
那样附加 CommandListener谢谢
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