Android Text-To-Speech抛出ActivityNotFoundException

Nia*_*yle 3 java android text-to-speech

我申请文本到语音到我的应用程序(如图所示的程序在这里).它在大多数设备上完美运行.但在某些设备中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
Run Code Online (Sandbox Code Playgroud)

这是我的简短代码:

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);
Run Code Online (Sandbox Code Playgroud)

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

高度赞赏.

Chu*_*ham 8

该错误表明该Activity名称android.speech.tts.engine.CHECK_TTS_DATA尚未作为ROM的一部分导出(注意:LG ROM已知存在问题.)为防止此错误,请执行以下操作:

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());
}
Run Code Online (Sandbox Code Playgroud)

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

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;
}
Run Code Online (Sandbox Code Playgroud)

哪里packageNameTextToSpeech.EngineclassNameTextToSpeech.Engine.ACTION_CHECK_TTS_DATA

  • @NiamhDoyle:使用第二个解决方案,这样如果Activity不可调用,您只需向LG用户显示一条消息,他们无法使用此特定TTS功能​​.这样,LG设备所有者可以向LG投诉,以便他们可以进行更改.就像我之前说过的那样,还有其他活动LG没有出口到他们的ROM,不仅给我带来了很多悲伤,还有其他开发者. (2认同)