BroadcastReceiver: can't instantiate class; no empty constructor

前端 未结 1 1888
面向向阳花
面向向阳花 2020-12-14 00:22

I have inner class as broadcast receiver:

public class ManualBacklightReceiver extends BroadcastReceiver {

    public static final String ACTION_MANUAL_BACK         


        
相关标签:
1条回答
  • 2020-12-14 01:22

    You need to declare your inner class as static. Otherwise, an inner class is associated with an instance of your outer class.

    Check out the Java Nested Classes tutorial for details. Here is a snippet:

    An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance. The next figure illustrates this idea.

    and:

    A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class. As a member of the OuterClass, a nested class can be declared private, public, protected, or package private. (Recall that outer classes can only be declared public or package private.)

    0 讨论(0)
提交回复
热议问题