使用 Yes, No 命令显示警报
在 J2me 应用程序中,我使用带有 yes, no 命令的警报。如果用户单击“是”命令,将显示“表单屏幕”,如果单击“否”命令,将显示“文本框”屏幕。但该代码不起作用。对于两个命令,仅显示文本框屏幕。
这是我的代码:
public Login(){
yes=new Command("Yes",Command.OK,1);
no=new Command("No",Command.CANCEL,1);
alert=new Alert("","Save The Changes?",null,AlertType.CONFIRMATION);
alert.setTimeout(Alert.FOREVER);
alert.addCommand(yes);
alert.addCommand(no);
textbox.setCommandListener(this);
alert.setCommanListener(this);
}
public void commandAction(Command command, Displayable displayable) {
if(displayable==textbox)
{
if(command==exit)
{
switchDisplayable(null,alert);
}
}
else if(displayable==alert)
{
if(command==no)
{
switchDisplayable(alert,getForm());
}
else if(command==yes)
{
switchDisplayable(alert,getTextbox());
}
}
}
我的错在哪里?
At the J2me application I used an alert with yes, no command. If user clicks the yes command Form Screen will be displayed and if clicks the no command TextBox screen will be displayed. But the code does not work. For two command only textbox screen will be displayed.
This is my code:
public Login(){
yes=new Command("Yes",Command.OK,1);
no=new Command("No",Command.CANCEL,1);
alert=new Alert("","Save The Changes?",null,AlertType.CONFIRMATION);
alert.setTimeout(Alert.FOREVER);
alert.addCommand(yes);
alert.addCommand(no);
textbox.setCommandListener(this);
alert.setCommanListener(this);
}
public void commandAction(Command command, Displayable displayable) {
if(displayable==textbox)
{
if(command==exit)
{
switchDisplayable(null,alert);
}
}
else if(displayable==alert)
{
if(command==no)
{
switchDisplayable(alert,getForm());
}
else if(command==yes)
{
switchDisplayable(alert,getTextbox());
}
}
}
Where is my fault?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的主要错误是我认为没有在您的 MIDlet 中使用适当的日志记录。除此之外,您发布的代码片段中没有明显的错误。
该错误很可能是由
getForm()
方法代码中出现问题引起的,但由于没有日志记录,您还必须检查其他可能性,例如命令侦听器或>没有
命令对象或alert
对象在代码中的其他地方被以某种方式更改。通过如下例所示的日志记录,您可以简单地在模拟器中运行 midlet 并检查控制台消息以查明预期代码是否已执行:
Your main fault here is I think not using appropriate logging in your MIDlet. Other than that, there are no evident mistakes in the code snippet you posted.
It is most likely that the error is caused by something going wrong in your
getForm()
method code, but since there is no logging, you have to also check other possibilities like eg that command listener orno
command object, oralert
object has been somehow changed somewhere else in your code.With logging like shown in example below, you could simply run your midlet in emulator and check console messages to find out whether expected code has been executed or not: