Java – Android text-to-speech throws an ActivityNotFoundException

Android text-to-speech throws an ActivityNotFoundException… here is a solution to the problem.

Android text-to-speech throws an ActivityNotFoundException

I applied text-to-speech to my application as shown in the procedure here )。 It works perfectly on most devices. However, in some devices, such as LG Optimus G, GK, L3 II, and Sky IM-A800S, the app activity stops unexpectedly with the following error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.appname/com.myapp.appname.ContentView}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.speech.tts.engine.CHECK_TTS_DATA }
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2067)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2092)
at android.app.ActivityThread.access$600(ActivityThread.java:138)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4827)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.speech.tts.engine.CHECK_TTS_DATA }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1568)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1439)
at android.app.Activity.startActivityForResult(Activity.java:3351)
at android.app.Activity.startActivityForResult(Activity.java:3312)
at android.support.v4.app.FragmentActivity.startActivityForResult(Unknown Source)
at viettien.kadict.ContentView.onCreate(Unknown Source)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1084)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2031)
... 11 more

Here is my shortcode :

private static final int MY_DATA_CHECK_CODE = 4;
private TextToSpeech tts;

onCreate()
    Fire off an intent to check if a TTS engine is installed
    Intent checkIntent = new Intent();
    checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

OnButtonClick
    SpeakText("Here is the word spoken");

onDestroy()
    tts.stop();
    tts.shutdown();

onActivityResult
    case MY_DATA_CHECK_CODE:

if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)  
        {
             success, create the TTS instance
            tts = new TextToSpeech(this, this);
        }
        else
        {
             missing data, install it
            Intent installIntent = new Intent();
            installIntent.setAction(
                    TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);  
            startActivity(installIntent);
        }

break;

onInit
    if (status == TextToSpeech.SUCCESS) {

int result = tts.setLanguage(Locale.US);

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

}     
    } else {
        Log.e("TTS", "Initilization Failed!");
    }

SpeakText
     tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);

Is this a ROM-related issue or is there a problem with my code?

Thank you very much for the help.

Solution

The error indicates that the activity named android.speech.tts.engine.CHECK_TTS_DATA has not been exported as part of the ROM (Note: LG ROM is known to have a problem. To prevent this error, do the following:

try {
   Intent intent = new Intent(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
   intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   context.startActivity(intent);   
} catch (ActivityNotFoundException e) {
   Log.e("Oops! The function is not available in your device." + e.fillInStackTrace());
}

Another method you can use is to first check if the package is callable, for example:

public static boolean isActivityCallable(Context context, String packageName, String className) {
    final Intent intent = new Intent();
    intent.setClassName(packageName, className);        
    List<ResolveInfo> list = getPackageManager(context).queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}

packageName is TextToSpeech.Engine and className is TextToSpeech.Engine.ACTION_CHECK_TTS_DATA

Related Problems and Solutions