12

I am new with Kotlin and little bit stack with intentService. Manifest shows me an error that my service doesn't contain default constructor, but inside service it looks ok and there are no errors.

Here is my intentService:

class MyService : IntentService {

    constructor(name:String?) : super(name) {
    }

    override fun onCreate() {
        super.onCreate()
    }

    override fun onHandleIntent(intent: Intent?) {
    }
}

I was also try another variant:

class MyService(name: String?) : IntentService(name) {

but when I try to run this service I still get an error:

java.lang.Class<com.test.test.MyService> has no zero argument constructor

Any ideas how to fix default constructor in Kotlin?

Thanks!

Andriy Antonov
  • 1,360
  • 2
  • 15
  • 29

1 Answers1

20

As explained here your service class needs to have parameterless consturctor. Change your implementation to in example:

class MyService : IntentService("MyService") {
    override fun onCreate() {
        super.onCreate()
    }

    override fun onHandleIntent(intent: Intent?) {
    }
}

The Android documentation on IntentService states that this name is only used for debugging:

name String: Used to name the worker thread, important only for debugging.

While not stated explicitly, on the mentioned documentation page, the framework needs to be able to instantiate your service class and expects there will be a parameterless constructor.

miensol
  • 39,733
  • 7
  • 116
  • 112