IntentService的默认构造函数(kotlin)

And*_*nov 10 android intentservice kotlin android-intentservice

我是Kotlin的新手,也是使用intentService的一点点堆栈.Manifest向我显示一个错误,我的服务不包含默认构造函数,但在服务内部它看起来没问题且没有错误.

这是我的intentService:

class MyService : IntentService {

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

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

    override fun onHandleIntent(intent: Intent?) {
    }
}
Run Code Online (Sandbox Code Playgroud)

我还尝试了另一种变体:

class MyService(name: String?) : IntentService(name) {
Run Code Online (Sandbox Code Playgroud)

但是当我尝试运行此服务时,我仍然会收到错误:

java.lang.Class<com.test.test.MyService> has no zero argument constructor
Run Code Online (Sandbox Code Playgroud)

任何想法如何修复Kotlin中的默认构造函数?

谢谢!

mie*_*sol 18

如此处所述,您的服务类需要具有无参数的consturctor.将您的实现更改为示例:

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

    override fun onHandleIntent(intent: Intent?) {
    }
}
Run Code Online (Sandbox Code Playgroud)

IntentService上的Android文档声明此名称仅用于调试:

name String:用于命名工作线程,仅对调试很重要.

虽然没有明确说明,但在提到的文档页面上,框架需要能够实例化您的服务类,并期望有一个无参数的构造函数.