No empty constructor when create a service

前端 未结 4 482
别跟我提以往
别跟我提以往 2020-12-01 08:40

I am struggling with this error:

08-08 11:42:53.179: E/AndroidRuntime(20288): Caused by: java.lang.InstantiationException: can\'t instantiate class co

相关标签:
4条回答
  • 2020-12-01 09:14

    If you have your Service declared as an Inner Class / Nested Class, you also need to make the class static

    Without that you´ll get the error even if your constructor is correct

    Explanation

    The reason for that is, you can only instantiate inner classes in the context of the outer class, so you would need to create an instance of the outer class first.

    Declaring your inner class static makes it independent from its outer class

    0 讨论(0)
  • 2020-12-01 09:16

    You need to add the default no-argument constructor to your ReminderService class. This is only implicitly added if you don't write a constructor of your own (which you have). See here: http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

    0 讨论(0)
  • 2020-12-01 09:25

    You need to add an empty constructor to your class i.e. one that takes no arguments:

    public ReminderService() {
        super("ReminderService");
    }
    

    Explanation from the documentation:

    The name is used to name the worker thread.

    NOTE: this is only applicable to intent service.

    0 讨论(0)
  • 2020-12-01 09:28

    Declare a default no-argument constructor for IntentService

    public class ReminderService extends IntentService {
        public ReminderService() {
          super("ReminderService");
        }
    }
    
    0 讨论(0)
提交回复
热议问题