2013-05-09 100 views
0

我收到threadid = 1的錯誤...我並不總是知道它,但我在某個時候在應用程序中的某個位置執行操作。不知道我的具體錯誤是什麼。我檢查了第164行,我沒有看到任何會導致結果的東西。也許我需要添加到我的清單中?這是我的LogCat。threadid = 1:線程以未捕獲的異常錯誤退出

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) 

這是我的主要活動代碼: 包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

帖子MainActivity代碼 – Gustek 2013-05-09 00:27:16

回答

0
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
if(matches!=null) { 
    for (String match : matches) 
    /* ... */ 
} 

比賽可以爲空。

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(); 
+0

奇怪......由於某種原因收到相同的錯誤...雖然我必須承認這是一個聰明的幾乎解決方案。 – 2013-05-09 00:42:18

+0

此外,第164行是爲(字符串匹配:匹配){如果(...等等...我確實改變了你所建議的方式,「兩個」和「一個」,所以不應該是一個問題現在。 – 2013-05-09 00:44:01

相關問題