2013-05-01 142 views
1

我有一個LWUIT類,它有一個List,列表本身包含一個Label作爲一個項目。Midlet不是抽象的,也不重寫抽象方法focusLost(com.sun.lwuit.Component)

我的想法只是當我專注於標籤時爲列表做出動作。

我得到以下錯誤,編譯類時:

匿名MIDlet的$ 2不是抽象的,在 的com.sun不重寫抽象 方法focusLost(com.sun.lwuit.Component)。 lwuit.events.FocusListener

String s = ("Focus me"); 
final com.sun.lwuit.Form f = new com.sun.lwuit.Form(); 
final com.sun.lwuit.List D = new com.sun.lwuit.List(); 
final com.sun.lwuit.Label l = new com.sun.lwuit.Label(s); 

D.addItem(l); 
f.addComponent(D); 

D.addFocusListener(new com.sun.lwuit.events.FocusListener() { 

    public void focusGained(com.sun.lwuit.Label l) 
    { 
    } 
    public void focusLost(com.sun.lwuit.Label l) 
    { 
    } 

}); 

回答

3

的什麼是錯的你的代碼是在錯誤信息所有的細節,你只需要仔細閱讀。你看,

  1. anonymous並簽署$Midlet$2告訴你什麼是錯的匿名類。
    在您的代碼段,這裏只有一個這樣的類:new com.sun.lwuit.events.FocusListener

  2. does not override abstract method focusLost(com.sun.lwuit.Component)意味着你的匿名類錯過了方法的定義有這樣的簽名(簽名是方法名稱和參數類型)

  3. 看您在那個匿名類中定義的方法更接近,編譯器是否抱怨過某種方法?乍一看,你可能會認爲它存在,有一種叫做focusLost的方法 - 但是(!)你需要記住,簽名不僅僅是方法名,還有參數類型 - 和(!)如果你看接近,你會發現參數類型不是錯誤信息中所要求的。

您的匿名類有方法focusLost(com.sun.lwuit.Label)但錯誤消息說,應該有方法具有不同的簽名(不同的參數類型) - focusLost(com.sun.lwuit.Component)

要修復此編譯錯誤,請將匿名類new com.sun.lwuit.events.FocusListener添加到具有所需簽名的方法:focusLost(com.sun.lwuit.Component)

相關問題