Android Text-To-Speech抛出ActivityNotFoundException

时间:2014-09-14 04:58:46

标签: java android text-to-speech

我将文字转语音应用到我的应用中(如here所示的过程)。它在大多数设备上完美运行。但在某些设备(例如LG Optimus G, GK, L3 II and Sky IM-A800S)中,应用活动意外停止,并出现以下错误:

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

这是我的简短代码:

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

这是与ROM相关的问题还是我的代码有问题?

非常感谢。

2 个答案:

答案 0 :(得分:8)

错误表明名为Activity的{​​{1}}尚未作为ROM的一部分导出(注意:已知LG ROM存在问题。)为防止此错误,请执行以下操作:< / p>

android.speech.tts.engine.CHECK_TTS_DATA

您可以使用的另一种方法是首先检查包是否可调用,如:

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

其中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; } packageNameTextToSpeech.EngineclassName

答案 1 :(得分:0)

检查您在该特定设备上运行的SDK。 TexttoSpeech API仅从Android 1.6 SDK引入。因此,如果您运行1.5 SDK,则可能会遇到此问题。

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

已经存在同样的问题。可能你需要稍微改变一下条件。

检查您的意图是否真正得到支持

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