Google Speech to text throw java.lang.ClassCastException

时间:2015-03-04 23:07:34

标签: android speech-to-text

**      *显示谷歌语音输入对话框      * * /

private void promptSpeechInput() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                getString(R.string.speech_prompt));
        try {
            startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
        } catch (ActivityNotFoundException a) {
            Toast.makeText(getApplicationContext(),
                    getString(R.string.speech_not_supported),
                    Toast.LENGTH_SHORT).show();
        }
    }

以下例外:

 03-04 16:33:13.281 W/Bundle  ( 3741): Key android.speech.extra.LANGUAGE expected String but value was a java.util.Locale.  The default value <null> was returned.
    03-04 16:33:13.281 W/Bundle  ( 3741): Attempt to cast generated internal exception:
    03-04 16:33:13.281 W/Bundle  ( 3741): java.lang.ClassCastException: java.util.Locale cannot be cast to java.lang.String
    03-04 16:33:13.281 W/Bundle  ( 3741): at android.os.BaseBundle.getString(BaseBundle.java:921)
    03-04 16:33:13.281 W/Bundle  ( 3741): at android.content.Intent.getStringExtra(Intent.java:4822)
    03-04 16:33:13.281 W/Bundle  ( 3741): at ihn.<init>(PG:97)
    03-04 16:33:13.281 W/Bundle  ( 3741): at com.google.android.voicesearch.intentapi.IntentApiActivity.onCreate(PG:37)
    03-04 16:33:13.281 W/Bundle  ( 3741): at android.app.Activity.performCreate(Activity.java:5953)
    03-04 16:33:13.281 W/Bundle  ( 3741): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
    03-04 16:33:13.281 W/Bundle  ( 3741): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
    03-04 16:33:13.281 W/Bundle  ( 3741): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
    03-04 16:33:13.281 W/Bundle  ( 3741): at android.app.ActivityThread.access$800(ActivityThread.java:148)
    03-04 16:33:13.281 W/Bundle  ( 3741): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
    03-04 16:33:13.281 W/Bundle  ( 3741): at android.os.Handler.dispatchMessage(Handler.java:102)
    03-04 16:33:13.281 W/Bundle  ( 3741): at android.os.Looper.loop(Looper.java:135)
    03-04 16:33:13.281 W/Bundle  ( 3741): at android.app.ActivityThread.main(ActivityThread.java:5312)
    03-04 16:33:13.281 W/Bundle  ( 3741): at java.lang.reflect.Method.invoke(Native Method)
    03-04 16:33:13.281 W/Bundle  ( 3741): at java.lang.reflect.Method.invoke(Method.java:372)
    03-04 16:33:13.281 W/Bundle  ( 3741): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)

1 个答案:

答案 0 :(得分:0)

尝试像

这样的硬编码语言
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");

而不是

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());

检查它是否有效,如果它有效,那么你就是Locale.getDefault());

的问题

您的应用可以通过发送RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS有序广播来查询支持的语言列表,如下所示:

Intent detailsIntent =  new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
    sendOrderedBroadcast(
            detailsIntent, null, new LanguageDetailsChecker(), null, Activity.RESULT_OK, null, null);

您可以编写广播接收器,如

public class LanguageDetailsChecker extends BroadcastReceiver
{
    private List<String> supportedLanguages;

    private String languagePreference;

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Bundle results = getResultExtras(true);
        if (results.containsKey(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE))
        {
            languagePreference =
                    results.getString(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE);
        }
        if (results.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES))
        {
            supportedLanguages =
                    results.getStringArrayList(
                            RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);
        }
    }
}

检查此链接 toolkit for using Android's Sensing capabilities