1

I have a project that uses TextToSpeech to serve certain features. Everything is fine until I try to run on Android version 9.0. It reported an error:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.speech.tts.TextToSpeech.setLanguage(java.util.Locale)' on a null object reference

These are the lines I wrote:

private void initializeTextToSpeech() {
        mytts= new TextToSpeech(HomeActivity.this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status ==0){
                    Toast.makeText(HomeActivity.this,
                            "There is no TTS engine on your device", Toast.LENGTH_LONG).show();
                    finish();
                }else {
                    int result = mytts.setLanguage(Locale.US);

                    if (result == TextToSpeech.LANG_MISSING_DATA
                            || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Log.e("TTS", "This Language is not supported");
                    } else {
                        Log.e("TTS", "success !");
                    }
                }
            }
        });
    }

And after I changed the condition status == 0 to become status! = TextToSpeech.SUCCESS then it announced that: "There is no TTS engine on your device". Seems like version 9.0 TTS is not supported?

0 Answers0