我收到了一个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
}
}
答案 0 :(得分:1)
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)
猜测第164行是
else if (match.equals("two")) testSound.start();
我怀疑你的match
有时会null
。如果是这样,match.equals...
代码将崩溃,原因很明显。最简单的解决方案是改变这一行:
else if (match.equals("two")) testSound.start();
更安全的形式:
else if ("two".equals(match)) testSound.start();
答案 1 :(得分:0)
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if(matches!=null) {
for (String match : matches)
/* ... */
}
如果没有匹配,匹配可以为null。