Android BroadcastReceiver:不能实例化类;没有空的构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10305261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BroadcastReceiver: can't instantiate class; no empty constructor
提问by artem
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:
安卓清单:
<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 = 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?
但是我有一个空的构造函数!为什么它不起作用?
回答by jarrad
You need to declare your inner class as static. Otherwise, an inner class is associated with an instanceof your outer class.
您需要将内部类声明为静态。否则,内部类与外部类的实例相关联。
Check out the Java Nested Classes tutorialfor 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 的成员,嵌套类可以声明为私有、公共、受保护或包私有。(回想一下,外部类只能声明为 public 或包私有。)