2016-11-24 288 views
4

我是新的Kotlin和intentService的一点点堆栈。清单显示我的错误,我的服务不包含默认的构造函数,但内部服务看起来不错,没有错误。IntentService(kotlin)的默认构造函数

这里是我的intentService:

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 

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

谢谢!

回答

7

如解释here您的服务类需要具有无参数构造函数。更改您的实现例子:

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

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

IntentService的Android文档指出,这名仅用于调试:

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

虽然没有明确规定,所提到的文档页面上,该框架的需求能够实例化你的服务类,并期望会有一个无参数的构造函数。

+0

非常感谢! –