实例内广播接收器类 [英] instantiate inner broadcast receiver class

查看:150
本文介绍了实例内广播接收器类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是改变了我的外部广播接收器类,以我的服务since..some Android的方法不能在静态的环境中使用。现在我收到一个错误无法实例活动ComponentInfo {COM ...}:显示java.lang.NullPointerException。怎么可能解决?下面是我的code嵌套的BroadcastReceiver类。

I just changed my external broadcast receiver class to my service since..some android method could not be used in static context. Now i receive an error Unable to instantiate activity ComponentInfo{com...}: java.lang.NullPointerException. How is it possible to fix? Below is my code for nested BroadcastReceiver class.

public class ServiceX extends Service {

private SharedPreferences settings = getSharedPreferences(PREFS, 0);
private SharedPreferences.Editor editor = settings.edit();

private static void setEnableNotification(int command) {
    if (command == 1)
        enableNotification = true;
    else
        enableNotification = false;

    editor.putBoolean("enableNotification", enableNotification);
      editor.commit();
}

public static class ReceiverX extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
            int enableNotification = intent.getIntExtra("EnableNotification", 0);

            if (enableNotification == 0)
                context.
                setEnableNotification(0);
            else if (enableNotification == 1)
                setEnableNotification(1);

}

}

下面是我如何instansiated内部类:

Below is how i instansiated the inner class:

public class ActivityX extends Activity{

private BroadcastReceiver receiver = security365Service.new NotifyServiceReceiver();

下面下面是我mainfest我在一些资料在网上看后改变:

Here below is my mainfest which i changed after looking at some sources online:

    <receiver android:name="com.milanix.services.ServiceX$ReceiverX" android:enabled="true">
    </receiver>

很抱歉,如果我的问题是愚蠢的。

Sorry if my question is dumb.

推荐答案

您在这里不能使用常规的内部类。这将需要一个静态内部类,这将让你回到原来的问题。所以,你需要解决你的问题这样或那样的Android的一些方法不能在静态的环境中使用。

You cannot use a regular inner class here. It would need to be a static inner class, which will get you back to your original problem. So, you need to solve your "some android method could not be used in static context" problem one way or another.

这篇关于实例内广播接收器类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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