我目前正在使用语音识别API为群组项目开发应用。我实际上是在一个月前首次实现它,从那时起它运作良好。 它非常简单,只需一个图像按钮即可在单击时启动语音识别活动。然后第一个结果由app“分析”(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
}
过去一个月一切都运行良好,昨天突然停止了(我确实更改了我的机器上的代码,但没有在我的手机上安装新版本,所以手机在一周前安装了相同的版本工作!)
非常感谢..
答案 0 :(得分:1)
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)
问题出在Main.java文件的第310行,方法为onActivityResult
。如果您有机会,可以仅发送 第310行作为评论吗?
另外,对super.onActivityResult(requestCode, resultCode, data);
的调用应该是您onActivityResult
方法中的第一个调用,因此请将该行打开。这可能不是你的崩溃,但它可能会导致问题。