-1

I'm a noob in Android dev.

I'm using TextToSpeech and it works well on several platforms, except on one particular phone (Running android 4.4.2).

There the instantiation of TTS gives a null-pointer exception:

private TextToSpeech mTTS;
// ...
mTTS = new TextToSpeech(this, mTTSInitListener); 

The Sdk version used is the minimum specified: 19 (and to my nuderstanding, TTS was added in API level 4 (thus Android 1.6)

android {
    compileSdkVersion 21
    buildToolsVersion '27.0.3'

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 21
        applicationId "myapp"
        versionCode 73
        versionName "1.1.0"

Question is: what can make TTS unavailable / non-instantiable ?

thx

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

First you should check that the device is capable of TTS or not. Like below code :

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
                            try {
                                startActivityForResult(intent, Constant.SEARCH_VOICE);
                                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                            } catch (ActivityNotFoundException a) {
                                Toast t = Toast.makeText(mContext, getResources().getString(R.string.speech_error), Toast.LENGTH_SHORT);
                                t.show();
                            }
Koustuv Ganguly
  • 897
  • 7
  • 21
  • Thx a lot Koustuv. Meanwhile I figured out that there was indeed no TTS installed on this device (I somehow imagined that it was embed in android). – Nike Tufjo May 31 '18 at 10:12
  • However, I had already tried this type of suggestion, but I then run in trouble with the "startActivityForResult" which is not recognized / or with a different signature in the compat lib... still struggling but getting closer ;-) – Nike Tufjo May 31 '18 at 10:14
  • **"startActivityForResult" which is not recognized** I assume you are not inside activity/fragment..! – Koustuv Ganguly May 31 '18 at 10:19
  • You're right, I was in a service. moved some code in the main activity and it is much better ! (yeah, total noob mistake ;-) Also installed a TTS on the device and no more NPE... (seems so obvious now) thx A LOT for your help, I am back on tracks now ! – Nike Tufjo May 31 '18 at 10:30
  • Is this check also valid for text-to-speech as this seems to check for speech-to-text support? – Markus Kauppinen May 31 '18 at 10:50