我收到 threadid=1 的错误...我并不总是得到它,但我有时会在应用程序中的某个时间点得到它。不确定我的具体错误是什么。我检查了第 164 行,但没有看到任何会导致结果的东西。也许我需要在清单中添加一些东西?这是我的 LogCat。
05-08 17:19:28.796: W/dalvikvm(8683): threadid=1: thread exiting with uncaught exception (group=0x40e37438)
05-08 17:19:28.806: E/AndroidRuntime(8683): FATAL EXCEPTION: main
05-08 17:19:28.806: E/AndroidRuntime(8683): java.lang.NullPointerException
05-08 17:19:28.806: E/AndroidRuntime(8683): at com.example.speech.MainActivity$SpeechListener.onResults(MainActivity.java:164)
05-08 17:19:28.806: E/AndroidRuntime(8683): at android.speech.SpeechRecognizer$InternalListener$1.handleMessage(SpeechRecognizer.java:442)
05-08 17:19:28.806: E/AndroidRuntime(8683): at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 17:19:28.806: E/AndroidRuntime(8683): at android.os.Looper.loop(Looper.java:137)
05-08 17:19:28.806: E/AndroidRuntime(8683): at android.app.ActivityThread.main(ActivityThread.java:4918)
05-08 17:19:28.806: E/AndroidRuntime(8683): at java.lang.reflect.Method.invokeNative(Native Method)
05-08 17:19:28.806: E/AndroidRuntime(8683): at java.lang.reflect.Method.invoke(Method.java:511)
05-08 17:19:28.806: E/AndroidRuntime(8683): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
05-08 17:19:28.806: E/AndroidRuntime(8683): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
05-08 17:19:28.806: E/AndroidRuntime(8683): at dalvik.system.NativeStart.main(Native Method)
这是我的主要活动代码:package com.example.speech;
import java.util.ArrayList;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.content.Intent;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.util.Log;
public class MainActivity extends Activity implements OnClickListener {
private SpeechRecognizer mSpeechRecognizer;
private Intent mSpeechRecognizerIntent;
boolean reseter = false;
private AudioManager mAudioManager;
private volatile boolean mNoSpeechCountDownOn;
MediaPlayer testSound;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean available = SpeechRecognizer.isRecognitionAvailable(this);
Log.d("Speech", "available = " + available);
testSound = MediaPlayer.create(MainActivity.this, R.raw.soundclip);
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(new SpeechListener());
mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 0);
mAudioManager = (AudioManager) getSystemService(this.AUDIO_SERVICE);
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
Log.d("speech", "Mute on");
try {
Thread.sleep(4000);
Log.d("speech", "repeat");
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
Log.d("speech", "Mute on");
} catch (InterruptedException e) {
}
}
private CountDownTimer mNoSpeechCountDown = new CountDownTimer(5000, 5000)
{
@Override
public void onTick(long millisUntilFinished)
{
}
@SuppressWarnings("synthetic-access")
@Override
public void onFinish()
{
mNoSpeechCountDownOn = false;
mSpeechRecognizer.cancel();
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class SpeechListener implements RecognitionListener {
@Override
public void onBeginningOfSpeech() {
if (mNoSpeechCountDownOn)
{
mNoSpeechCountDownOn = false;
mNoSpeechCountDown.cancel();
}
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) {
if (mNoSpeechCountDownOn)
{
mNoSpeechCountDownOn = false;
mNoSpeechCountDown.cancel();
}
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) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
{
mNoSpeechCountDownOn = true;
mNoSpeechCountDown.start();
mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
}
Log.d("Speech", "onReadyForSpeech");
try {
Thread.sleep(6000);
Log.d("speech", "repeat");
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
Log.d("speech", "Mute on");
} catch (InterruptedException e) {
}}
@Override
public void onResults(Bundle results) {
Log.d("Speech", "results");
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for (String match : matches) { if (match.equals("one")) testSound.start();
else if (match.equals("two")) testSound.start();
}
// Do whatever you want here
try {
Thread.sleep(1);
Log.d("speech", "repeat");
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
Log.d("speech", "Mute on");
} catch (InterruptedException e) {
}
}
@Override
public void onRmsChanged(float rmsdB) {
//Log.d("Speech", "onRmsChanged");
}
}
@Override
public void onClick(View arg0) {
//must keep the onClick
}
}