简体   繁体   English

Android ::为什么要使用getBooleanExtra java.lang.NullPointerException

[英]Android :: why getBooleanExtra java.lang.NullPointerException

PROBLEM 问题

As topic mention, I don't know why getBooleanExtra() java.lang.NullPointerException. 正如主题所述,我不知道为什么要使用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. 但是,从下面的代码中可以看到,每个getBooleanExtra()都有一个默认值为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() 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. 问题在于,甚至服务也可能被系统杀死并重新启动,这就是为什么您拥有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. 这是官方android开发人员博客的链接。
http://android-developers.blogspot.com/2010/02/service-api-changes-starting-with.html 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. START_STICKY 与以前的行为基本相同,在该行为中,服务保持“启动”状态,稍后将由系统重新启动。 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. 与平台先前版本的唯一区别在于,如果由于平台进程被终止而重新启动平台,则将在服务的下一个实例中使用空Intent调用onStartCommand()而不是根本不调用它。 Services that use this mode should always check for this case and deal with it appropriately. 使用此模式的服务应始终检查这种情况并进行适当处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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