2014-05-06 19 views
0

我正在学习为诺基亚S40设备开发一些基本应用程序。我已经创建了一些供用户使用的命令。问题是,命令'不'工作,但'是'什么也不做。为什么是这样?示例如下:为什么我的commandListener不工作?

final Form form = new Form("Form"); 
    form.append("Update?"); 

    final Command yes = new Command("Yes", Command.OK, 1); 
    final Command no = new Command("No", Command.CANCEL, 1); 

    form.addCommand(yes); 
    form.addCommand(no); 
    form.setCommandListener(new CommandListener() { 

     public void commandAction(Command arg0, Displayable arg1) { 
      // TODO Auto-generated method stub 

      if (arg0 == yes) { 
       System.out.println("Yes pressed"); 
      } else if (arg0 == no) { 
       System.out.println("No Pressed"); 
      } 
     } 
    }); 

任何指针或建议,将不胜感激。

回答

0

使用等号不能比较两个对象。

您必须使用Object.equals(anotherObject)方法。

因此,与if (arg0.equals(yes))

if (arg0 == no)if (arg0.equals(no))取代if (arg0 == yes)