问题
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