BroadcastReceiver:不能实例化类;没有空构造函数

时间:2022-02-10 12:45:46

I have inner class as broadcast receiver:

我有内部类作为广播接收器:

public class ManualBacklightReceiver extends BroadcastReceiver {

    public static final String ACTION_MANUAL_BACKLIGHT = "com.android.systemui.statusbar.powerwidget.MANUAL_BACKLIGHT";

    public ManualBacklightReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("ManualBacklightReceiver", intent.getAction());
    }

};

AndroidManifest:

AndroidManifest:

<receiver android:name=".statusbar.powerwidget.PowerWidgetGrid$ManualBacklightReceiver">
        <intent-filter>
            <action android:name="com.android.systemui.statusbar.powerwidget.MANUAL_BACKLIGHT"/>
        </intent-filter>            
    </receiver>

And when I send the intent with this code: Intent intent = new Intent();

当我用此代码发送意图:intent intent intent = new intent ();

intent.setAction("com.android.systemui.statusbar.powerwidget.MANUAL_BACKLIGHT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.sendBroadcast(intent);

I get these exceptions:

我得到这些异常:

java.lang.RuntimeException: Unable to instantiate receiver com.android.systemui.statusbar.powerwidget.PowerWidgetGrid$ManualBacklightReceiver:
java.lang.InstantiationException: can't instantiate class com.android.systemui.statusbar.powerwidget.PowerWidgetGrid$ManualBacklightReceiver; no empty constructor
Caused by: java.lang.InstantiationException: can't instantiate class com.android.systemui.statusbar.powerwidget.PowerWidgetGrid$ManualBacklightReceiver; no empty constructor

But I have an empty constructor! Why it doesn't work?

但是我有一个空的构造函数!为什么它不工作?

1 个解决方案

#1


119  

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:

详细信息请参阅Java嵌套类教程。这是一个片段:

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.

InnerClass的实例只能存在于OuterClass的实例中,并且可以直接访问其封闭实例的方法和字段。下一个图说明了这个想法。

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.)

嵌套类是其封闭类的成员。非静态嵌套类(内部类)可以访问封闭类的其他成员,即使它们被声明为私有。静态嵌套类不能访问封闭类的其他成员。作为OuterClass的成员,一个嵌套类可以被声明为私有、公共、保护或包私有。(回想一下,外部类只能声明为公有或包为私有。)

#1


119  

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:

详细信息请参阅Java嵌套类教程。这是一个片段:

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.

InnerClass的实例只能存在于OuterClass的实例中,并且可以直接访问其封闭实例的方法和字段。下一个图说明了这个想法。

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.)

嵌套类是其封闭类的成员。非静态嵌套类(内部类)可以访问封闭类的其他成员,即使它们被声明为私有。静态嵌套类不能访问封闭类的其他成员。作为OuterClass的成员,一个嵌套类可以被声明为私有、公共、保护或包私有。(回想一下,外部类只能声明为公有或包为私有。)