From 135f852598689f7a5afd695b008a0d41c3fc24e9 Mon Sep 17 00:00:00 2001 From: "Reece H. Dunn" Date: Sun, 31 May 2015 19:51:03 +0100 Subject: [PATCH] getTtsLanguage: workaround NPE in >= Lollipop. When there is no TTS engine selected, mTts.getVoice() will throw a NullPointerException. This is a workaround to cope with this bug in Android. --- android/src/com/reecedunn/espeak/eSpeakActivity.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/android/src/com/reecedunn/espeak/eSpeakActivity.java b/android/src/com/reecedunn/espeak/eSpeakActivity.java index 66a2a3ab..ac63a0ae 100644 --- a/android/src/com/reecedunn/espeak/eSpeakActivity.java +++ b/android/src/com/reecedunn/espeak/eSpeakActivity.java @@ -198,7 +198,14 @@ public class eSpeakActivity extends Activity { private Locale getTtsLanguage() { if (mTts != null) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - android.speech.tts.Voice voice = mTts.getVoice(); + android.speech.tts.Voice voice = null; + try { + voice = mTts.getVoice(); + } catch (NullPointerException e) { + // Android Lollipop and later may throw a NullPointerException + // if a voice is not selected. + return null; + } if (voice != null) { return voice.getLocale(); }