简体   繁体   中英

Android :: why getBooleanExtra java.lang.NullPointerException

PROBLEM

As topic mention, I don't know why getBooleanExtra() java.lang.NullPointerException.

I understand that sometimes intent may not contains extras.

However, from the below code as you can see there is a default value for each getBooleanExtra() which is false.

So, that's the reason why I don't understand. please advice. thx!

SOME CODE FROM MY SERVICE CLASS

 @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("onStartCommand()->","Intent Service.... " + intent);

        final boolean SLEEP_MODE_ON  = intent.getBooleanExtra("SLEEP_MODE_ON",false);
        final boolean SLEEP_MODE_OFF = intent.getBooleanExtra("SLEEP_MODE_OFF",false);

        Thread thread = new Thread(new Runnable(){
            @Override
            public void run() {
                connectIfNecessary();

                if (SLEEP_MODE_ON){
                    doSleepMode_on();
                } else if (SLEEP_MODE_OFF) {
                    doSleepMode_off();
                }

            }
        });
        thread.start();

        return START_STICKY;
    }

EDIT as some ask Where I call My service?? First, from activity. Second, from broadcastReceiver

ACTIVITY in onCreate()

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Remove title bar
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);

    startService(new Intent(this,mqttPushService.class)); //Setup MQTT Service


}//END of onCreate()

BroadcastReceiver

public class SleepModeReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        Intent sleepModeIntent;
        int broadcastID = intent.getIntExtra("BROADCAST_ID",0);

        switch (broadcastID) {
            case DataManager.BROADCAST_ID_SLEEP_MODE_START :
                sleepModeIntent = new Intent(context, mqttPushService.class);
                sleepModeIntent.putExtra("SLEEP_MODE_ON",true);
                context.startService(sleepModeIntent);
                break;
            case DataManager.BROADCAST_ID_SLEEP_MODE_STOP :
                sleepModeIntent = new Intent(context, mqttPushService.class);
                sleepModeIntent.putExtra("SLEEP_MODE_OFF",true);
                context.startService(sleepModeIntent);
                break;
        }

    }
}

I ran into this recently. The issue is that even services can be killed and restarted by the system , thats why you have START_STICKY. Unlike when you start the service and you pass a valid intent, when the system restarts the service, the intent is null. I just check for a null intent before trying to extract any extras.

Here is the link to the official android developers blog.
http://android-developers.blogspot.com/2010/02/service-api-changes-starting-with.html

and here is the paragraph which basically says what I say above

START_STICKY is basically the same as the previous behavior, where the service is left "started" and will later be restarted by the system. The only difference from previous versions of the platform is that it if it gets restarted because its process is killed, onStartCommand() will be called on the next instance of the service with a null Intent instead of not being called at all. Services that use this mode should always check for this case and deal with it appropriately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM