我正在为我的应用程序进行语音识别,我尝试了这段代码,但在我的 logcat 中出现错误,因为意图不能在 android 中为空异常
public class SpeechRecognizerActivity extends Activity {
/** Called when the activity is first created. */
String a;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView txt=(TextView)findViewById(R.id.textView1);
class MyRecognitionListener implements RecognitionListener {
@Override
public void onBeginningOfSpeech() {
Log.d("Speech", "onBeginningOfSpeech");
}
@Override
public void onBufferReceived(byte[] buffer) {
Log.d("Speech", "onBufferReceived");
}
@Override
public void onEndOfSpeech() {
Log.d("Speech", "onEndOfSpeech");
}
@Override
public void onError(int error) {
Log.d("Speech", "onError");
}
@Override
public void onEvent(int eventType, Bundle params) {
Log.d("Speech", "onEvent");
}
@Override
public void onPartialResults(Bundle partialResults) {
Log.d("Speech", "onPartialResults");
}
@Override
public void onReadyForSpeech(Bundle params) {
Log.d("Speech", "onReadyForSpeech");
}
@Override
public void onResults(Bundle results) {
Log.d("Speech", "onResults");
ArrayList<String> strlist = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for (int i = 0; i < strlist.size();i++ ) {
Log.d("Speech", "result=" + strlist.get(i));
}
}
@Override
public void onRmsChanged(float rmsdB) {
Log.d("Speech", "onRmsChanged");
}
}
SpeechRecognizer sr = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
MyRecognitionListener listener = new MyRecognitionListener();
sr.setRecognitionListener(listener);
sr.startListening(RecognizerIntent.getVoiceDetailsIntent(getApplicationContext()));
}
}
请建议我,我犯了什么错误,或者在运行程序之前必须在模拟器中设置一些设置。