Android IntentService(kotlin)的默认构造函数
我是Kotlin和intentService的新成员。清单向我显示了一个错误,我的服务不包含默认构造函数,但在服务内部它看起来正常,并且没有错误 这是我的意向服务:Android IntentService(kotlin)的默认构造函数,android,kotlin,intentservice,android-intentservice,Android,Kotlin,Intentservice,Android Intentservice,我是Kotlin和intentService的新成员。清单向我显示了一个错误,我的服务不包含默认构造函数,但在服务内部它看起来正常,并且没有错误 这是我的意向服务: class MyService : IntentService { constructor(name:String?) : super(name) { } override fun onCreate() { super.onCreate() } override fun
class MyService : IntentService {
constructor(name:String?) : super(name) {
}
override fun onCreate() {
super.onCreate()
}
override fun onHandleIntent(intent: Intent?) {
}
}
我还尝试了另一种变体:
class MyService(name: String?) : IntentService(name) {
但当我尝试运行此服务时,仍会出现错误:
java.lang.Class<com.test.test.MyService> has no zero argument constructor
java.lang.Class没有零参数构造函数
如何修复Kotlin中的默认构造函数
谢谢 如前所述,您的服务类需要具有无参数constructor。在示例中将实现更改为:
class MyService : IntentService("MyService") {
override fun onCreate() {
super.onCreate()
}
override fun onHandleIntent(intent: Intent?) {
}
}
上的Android文档声明此名称仅用于调试:
name
String
:用于命名工作线程,仅对调试很重要
虽然没有明确说明,但在提到的文档页面上,框架需要能够实例化您的服务类,并期望有一个无参数构造函数