mducc
mducc

Reputation: 743

Service error "... has no zero argument constructor"

I want start a service, I use:

inner class HomeService : Service() {
    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

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

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        notification(light.toString())
        return START_STICKY
    }

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

start it:

val i = Intent(this@Main3Activity, HomeService()::class.java)
startService(i)

after my app crashed, logcat return:

java.lang.Class has no zero argument constructor at android.app.ActivityThread.handleCreateService(ActivityThread.java:3201) at android.app.ActivityThread.-wrap5(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1586) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6186) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779) Caused by: java.lang.InstantiationException: java.lang.Class has no zero argument constructor at java.lang.Class.newInstance(Native Method) at android.app.ActivityThread.handleCreateService(ActivityThread.java:3198) at android.app.ActivityThread.-wrap5(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1586)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6186)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

what should I do?

Upvotes: 3

Views: 8079

Answers (2)

Brijesh Joshi
Brijesh Joshi

Reputation: 1857

class HomeService() : Service() {
    constructor(context: Context) : this() {
        Log.d("TAG","ANOTHER CONSTRUCTOR")
    }
}

Now you can use this service by two ways

  1. val intent = Intent(this@Main3Activity, HomeService::class.java)
  2. val intent = Intent(this@Main3Activity, HomeService(context)::class.java)

Upvotes: 3

zsmb13
zsmb13

Reputation: 89578

You're creating an instance by hand by writing down HomeService() inside your Intent creation code. Creating the service is the task of the framework, and you can pass its class without creating an instance:

val i = Intent(this@Main3Activity, HomeService::class.java)
startService(i)

Update:

In addition to the above, your class also can't be an inner class. It can be nested, but it can't be an inner class, because those hold a reference to the class that they're inside. This means that the system can't create an instance of your service on its own - it has no way of providing an outer class for it to reference.

Upvotes: 6

Related Questions