ボクココ

個人開発に関するテックブログ

IntentService で Unable to instantiate service

IntentService のサンプルを作っていて、上記エラーが出たのでメモ。 具体的なエラーはこんな感じ java.lang.RuntimeException: Unable to instantiate service com.sample.SampleService: java.lang.InstantiationException: com.sample.SampleService

原因はEclipse のコード生成を使って IntentService のコンストラクタにname引数があったためでだった。

public class SampleService extends IntentService {

    public SampleService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.v("test", "called sample service");
    }

}

コンストラクタの引数を消して

public class SampleService extends IntentService {

    public SampleService() {
        super("SampleService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.v("test", "called sample service");
    }

}

これでOK。