Niamh Doyle
Niamh Doyle

Reputation: 1949

Android Text-To-Speech throws ActivityNotFoundException

I applied text-to-speech to my app (as the procedure shown here). It works flawlessly on most of the devices. But 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

And here is my brief code:

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 problem or there is something wrong with my code?

Help highly appreciated.

Upvotes: 4

Views: 2932

Answers (2)

ChuongPham
ChuongPham

Reputation: 4801

The error indicated that the Activity named android.speech.tts.engine.CHECK_TTS_DATA has not been exported as part of the ROM (Note: LG ROMs have been known to be problematic.) To prevent this error just do this:

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 check first whether a package is callable, like:

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;
}

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

Upvotes: 12

Prasanth Louis
Prasanth Louis

Reputation: 5076

Check the SDK that you are running in that particular device. TexttoSpeech API has been introduced only from Android 1.6 SDK. So if you running 1.5 SDK you might face this issue.

http://android-developers.blogspot.in/2009/09/introduction-to-text-to-speech-in.html

There has been already an issue that has been filed on the same. Probably you need to change the conditions a bit.

Check if your intent is actually supported

PackageManager pm = getPackageManager();
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
ResolveInfo resolveInfo = pm.resolveActivity( installIntent, PackageManager.MATCH_DEFAULT_ONLY );

if( resolveInfo == null ) {
   // Not able to find the activity which should be started for this intent
} else {
   startActivity( installIntent );
}

Upvotes: 1

Related Questions