我有内部类作为广播接收器:
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);
我得到这些例外:
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嵌套类”教程了解详细信息。 这是一个片段:
InnerClass的实例只能存在于OuterClass的一个实例中,并且可以直接访问其封闭实例的方法和字段。 下图说明了这个想法。
和:
嵌套类是其封闭类的成员。 非静态嵌套类(内部类)可以访问封闭类的其他成员,即使它们被声明为私有。 静态嵌套类不能访问封闭类的其他成员。 作为OuterClass的成员,可以将嵌套类声明为private,public,protected或package private。 (回想一下,只能将外部类声明为public或package private。)
以上就是android开发分享BroadcastReceiver:不能实例化类; 没有空的构造函数相关内容,想了解更多android开发(异常处理)及android游戏开发关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/addevelopment/513016.html