BroadcastReceiver的:无法实例类;没有空的构造 [英] BroadcastReceiver: can't instantiate class; no empty constructor

查看:179
本文介绍了BroadcastReceiver的:无法实例类;没有空的构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有内部类的广播接收器:

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>

当我送的意图与此code: 意向意图=新的意图();

And when I send the intent with this code: 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?

推荐答案

您需要声明的内部类是静态的。否则,一个内部类与一个的实例相关联的你的外部类。

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

查看的Java内部类教程了解详细信息。这里是一个片段:

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

将InnerClass的一个实例可以实例中只存在   OuterClass,可直接访问方式和领域的   封闭实例。下图说明了这个想法。

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.

一个嵌套类是它包围类的成员。非静态嵌套   类(内部类)获得的包围中的其他成员   一流的,即使被声明为private。静态嵌套类不   访问封装类的其他成员。作为一个部件   该OuterClass,嵌套类可以被声明为私有,公有,   保护或包专用。 (回想一下,外部类只能是   声明为public或包专用。)

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

这篇关于BroadcastReceiver的:无法实例类;没有空的构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆