I got this error because I was using an object variable inside the main loop:
class SpeechRecognition implements RecognitionListener {
private SpeechRecognizer recognizer;
public void transcribe (Activity activity) {
new Handler(Looper.getMainLooper()).post(() -> {
this.speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this.activity);
this.speechRecognizer.setRecognitionListener(this.listener);
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
this.speechRecognizer.startListening(intent);
}
}
[...]
}
Solution:
class SpeechRecognitionRunnable implements Runnable {
private volatile SpeechRecognizer speechRecognizer;
private RecognitionListener listener;
private Activity activity;
public SpeechRecognitionRunnable (RecognitionListener listener, Activity activity) {
this.listener = listener;
this.activity = activity;
}
@Override
public void run() {
this.speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this.activity);
this.speechRecognizer.setRecognitionListener(this.listener);
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
this.speechRecognizer.startListening(intent);
}
public SpeechRecognizer getSpeechRecognizer() {
return this.speechRecognizer;
}
}
class SpeechRecognition implements RecognitionListener {
private SpeechRecognizer recognizer;
public void transcribe (Activity activity) {
new Handler(Looper.getMainLooper()).post(new SpeechRecognitionRunnable(this, activity)
}
[...]
}