2013-03-19 66 views
1

我想使用遠程服務來構建藍牙聊天(如Android樣本之一)。但自從我上次修改以來,應用程序無法啓動,並且日誌顯示我的服務的錯誤no empty constructor。我讀過this post,但我不能添加任何空的構造函數。必須有一些我做錯了,或者它不適用於遠程服務。沒有空的構造函數

這是我的構造函數:

public RemoteService(Context context, Handler handler) { 
    mAdapter = BluetoothAdapter.getDefaultAdapter(); 
    mState = STATE_NONE; 
} 

有人能告訴我什麼是錯呢?

+0

爲什麼你不能添加一個空的構造函數? – Neil 2013-03-19 11:26:22

+0

您必須添加一個無參數構造函數 – 2013-03-19 11:27:19

回答

1

嘗試:

public RemoteService() { 
    super(); 
} 

除非你的業務規則規定你不能真的有一個空的構造。那麼你在某個地方呼叫new RemoteService(),當你不應該。

0

您必須提供默認的構造函數,因爲服務需要一個默認的構造函數,然後您可以使用參數化的構造函數。

public class ReminderService extends IntentService { 

    ---------------------------- 
    public ReminderService() // Need to add the default constructor 
    { 
     super("ReminderService"); // Calling the super constructor 
    } 

    public RemoteService(Context context, Handler handler) //Then use your own constructor 
    { 
    mAdapter = BluetoothAdapter.getDefaultAdapter(); 
    mState = STATE_NONE; 
    } 
    ---------------------------------- 

} 

試試這個,讓我知道如果你仍然有任何問題。

0

感謝您的回答,但與此同時,有人幫我清理了代碼,刪除了構造函數並做了其他一些事情,但我並不瞭解所有的東西。

我很抱歉,我無法解釋到底什麼是錯的,但我可以說非參數構造函數產生錯誤。

相關問題