Service error “… has no zero argument constructor”

Internet of Things IoT Frameworks 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating
_x000D_ _x000D_ 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?

Posted on 16 Aug 2022, this text provides information on IoT Frameworks related to Internet of Things. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
_x000D_ 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.

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Important Internet of Things Links