instantiate inner broadcast receiver class

亡梦爱人 提交于 2020-01-16 08:49:07

问题


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

}

}

Below is how i instansiated the inner class:

public class ActivityX extends Activity{

private BroadcastReceiver receiver = security365Service.new NotifyServiceReceiver();

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.


回答1:


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.



来源:https://stackoverflow.com/questions/9203638/instantiate-inner-broadcast-receiver-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!