【发布时间】:2012-03-03 16:37:46
【问题描述】:
我目前正在为一个小组项目开发一个使用语音识别 API 的应用程序。我实际上是在大约一个月前首次实施它,从那时起它运行得很好。 这很简单,只有一个图像按钮,单击时会启动语音识别活动。然后应用程序“分析”第一个结果(switch 语句检查它包含哪些单词)。
我遇到问题的部分是,当我周四在手机上安装该应用时,它运行良好。但是从昨天开始就不行了!当点击按钮时,语音识别界面打开,它会记录我告诉它的内容,然后屏幕变黑并崩溃。
这是我运行应用程序时得到的 logcat:
03-03 16:16:05.365: E/AndroidRuntime(12262): FATAL EXCEPTION: main
03-03 16:16:05.365: E/AndroidRuntime(12262): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1234, result=-1, data=Intent { (has extras) }} to activity {com.brice/com.brice.Main}: java.lang.NullPointerException
03-03 16:16:05.365: E/AndroidRuntime(12262): at android.app.ActivityThread.deliverResults(ActivityThread.java:2992)
03-03 16:16:05.365: E/AndroidRuntime(12262): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3035)
03-03 16:16:05.365: E/AndroidRuntime(12262): at android.app.ActivityThread.access$1100(ActivityThread.java:127)
03-03 16:16:05.365: E/AndroidRuntime(12262): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1189)
03-03 16:16:05.365: E/AndroidRuntime(12262): at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 16:16:05.365: E/AndroidRuntime(12262): at android.os.Looper.loop(Looper.java:137)
03-03 16:16:05.365: E/AndroidRuntime(12262): at android.app.ActivityThread.main(ActivityThread.java:4507)
03-03 16:16:05.365: E/AndroidRuntime(12262): at java.lang.reflect.Method.invokeNative(Native Method)
03-03 16:16:05.365: E/AndroidRuntime(12262): at java.lang.reflect.Method.invoke(Method.java:511)
03-03 16:16:05.365: E/AndroidRuntime(12262): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
03-03 16:16:05.365: E/AndroidRuntime(12262): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
03-03 16:16:05.365: E/AndroidRuntime(12262): at dalvik.system.NativeStart.main(Native Method)
03-03 16:16:05.365: E/AndroidRuntime(12262): Caused by: java.lang.NullPointerException
03-03 16:16:05.365: E/AndroidRuntime(12262): at com.brice.Main.onActivityResult(Main.java:310)
03-03 16:16:05.365: E/AndroidRuntime(12262): at android.app.Activity.dispatchActivityResult(Activity.java:4649)
03-03 16:16:05.365: E/AndroidRuntime(12262): at android.app.ActivityThread.deliverResults(ActivityThread.java:2988)
03-03 16:16:05.365: E/AndroidRuntime(12262): ... 11 more
我猜结果=-1 是问题所在,但我不知道如何处理...
这是处理语音识别部分的代码部分:
/**
* Fire an intent to start the voice recognition activity.
*/
private void startVoiceRecognitionActivity() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1); //In order to only get the best result
startActivityForResult(intent, REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
boolean success=false; //To check if the STT worked properly
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
{
// Populate the wordsList with the String value the recognition engine thought it heard
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
//Outputs the message
voiceIn.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, matches));
success=true;
}
//Get the result as a String
first = (String) voiceIn.getItemAtPosition(0);
super.onActivityResult(requestCode, resultCode, data);
if (success){
if (first.contains("slideshow")){
loadSlideshow();
}
else{
messageTo = first; //Take the speech as the message
//Post the message to the recipient
Functions.postData(messageTo, recipient);
//Notify Granny that the message is sent
//Toast.makeText(Main.this, "Message Sent", Toast.LENGTH_LONG).show(); //Text Notifier
tts.speak("Your message has been sent", TextToSpeech.QUEUE_ADD, null); //Speech Notifier
}
}
else{ //If the Speech to Text had a problem, notify the user
tts.speak("There was a problem with the Speech Recognition Service, you should try again", TextToSpeech.QUEUE_ADD, null); //Speech Notifier
}
上个月再次一切正常,昨天突然停止(我确实更改了我机器上的代码,但没有在我的手机上安装新版本,所以手机安装了一周前使用的相同版本工作!)
非常感谢..
【问题讨论】:
标签: android speech-recognition