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

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>

当我用以下代码发送意图时:
Intent intent = new Intent();
intent.setAction("com.android.systemui.statusbar.powerwidget.MANUAL_BACKLIGHT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.sendBroadcast(intent);

我得到这些异常(exception):
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

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

最佳答案

您需要将内部类声明为静态。否则,内部类将与外部类的实例相关联。

查看Java Nested Classes tutorial以获得详细信息。这是一个片段:



和:

关于android - BroadcastReceiver:无法实例化类;没有空的构造函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10305261/

10-12 01:34