2013-06-19 53 views
0

我試圖在服務實現手機狀態監聽器和我得到的錯誤:語法錯誤「擴展」拋出預期 - 在服務實施PhoneStateListener時

Syntax error on token "extends", throws expected 

但是當我改變extendsthrows我得到3級新的錯誤,指出:

EndCallListener cannot be resolved to a type 
EndCallListener cannot be resolved to a type 
No exception of type PhoneStateListener can be thrown; an exception type must be a subclass of Throwable 

有什麼建議?

阿曼尼斯旺

public void onCreate() extends PhoneStateListener { 
    EndCallListener callListener = new EndCallListener(); 
    TelephonyManager mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE); 
    mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE); 
} 

public void onCallStateChanged(int state, String incomingNumber) { 
    if(TelephonyManager.CALL_STATE_OFFHOOK == state) { 
    // set number of calls to 1 in SharedPreferences      
    SharedPreferences pref = getApplicationContext() 
    .getSharedPreferences("DataCountService", 0); 
    Editor editor = pref.edit(); 
    editor.putString("calls", "1"); 
    editor.commit(); 
    } 

回答

0

如果是的onCreate作爲返回類型爲「無效」,並在名稱後括號INFACT的方法,指出,你不能使用有延伸。只有班級可以從其他類別中刪除。

public class ClassName extends ParentClassName { //class code } 

在這裏您可以找到關於繼承的神諭toutorial