Good folks, I have my following onDestroy() method:
@Override
protected void onDestroy() {
super.onDestroy();
mSpeechRecognizer.cancel();
mSpeechRecognizer.destroy();
}
According to the Google console, it is causing a NPE sometimes, I have checked and yes. I think the reason is that sometimes I am destroying a process that has not started and I know that the solution is with an if, but I don't know how to tell it to destroy it when it has started.
In this case the problem is that sometimes the instance of
SpeechRecognizer
may never have been initialized or was active and now has a null value.In this case when you close your application or
Activity
you must validate the case in which itmSpeechRecognizer
has a null value:If it has been started,
mSpeechRecognizer
it will not be null so the following lines will do the job in this case.Why isn't the stopListening() method working? android java