2013-05-09 5 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; 그 라인 (164)을 추측

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

답변

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

일치 null의 경우도있다.

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) 

나는 당신의 match 때때로 null 될 수 있습니다 의심

else if (match.equals("two")) testSound.start(); 

입니다. 그렇다면 match.equals... 코드가 꽤 많은 분명한 이유로 인해 충돌합니다.

else if (match.equals("two")) testSound.start(); 

더 안전한 형태 : 가장 간단한 솔루션에서,이 라인을 변경하는 것입니다 채우는 것이없는 경우

else if ("two".equals(match)) testSound.start(); 
+0

이상한 .... 어떤 이유로 든 같은 오류가 발생했습니다 ... 나는 그것이 영리한 거의 해결책이라고 인정해야하지만. –

+0

또한 164 행은 (문자열 일치 : 일치) {if (.... so on ... "두"와 "하나"를 제안한 방식으로 변경 했으므로 문제가 될 수 없습니다 지금. –

관련 문제