2017-10-12 64 views
2

我得到这个错误时,即时通讯在Android模拟器按home键机器人:无法暂停活动:java.lang.IllegalArgumentException异常:服务没有注册:

了java.lang.RuntimeException:无法暂停活动{COM .thesis.dic_ta_han/com.thesis.dic_ta_han.MainActivity}:java.lang.IllegalArgumentException:未注册的服务:[email protected] at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3260) at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3219) at android.app.ActivityThread.handlePauseA (ActivityThread.java:3194) at android.app.ActivityThread.access $ 1000(ActivityThread.java:151) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1314) at android.os.Handler .dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method .invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903) at com。java.lang.reflect.Method.invoke(Method.java:372) android.internal .os.ZygoteInit.main(ZygoteInit.java:698) 引起:java.lang.IllegalArgumentException:服务未注册:[email protected] at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java: 1029) at android.app.ContextImpl.unbindService(ContextImpl.java:1816) at android.content.ContextWrapper.unbindService(ContextWrapper.java:551) at android.speech.SpeechRecognizer.destroy(SpeechRecognizer.java:408) at com.thesis.dic_ta_han.MainActivity.onPause(MainActivity.java:83) at android.app.Activity.performPause(Activity.java:6101) at android.app.Instrumentat在android.app.ActivityThread上的android.app.ActivityThread.performPauseActivity(ActivityThread.java:3219) 上的android.app.ActivityThread.performPauseActivity(ActivityThread.java:3246) (ion.callActivityOnPause(Instrumentation.java:1310) 。 handlePauseActivity(ActivityThread.java:3194) at android.app.ActivityThread.access $ 1000(ActivityThread.java:151) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1314) at android.os.Handler .dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) (Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit .java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit。的java:698)

MainActivity的代码:

public class MainActivity extends Activity implements RecognitionListener { 

private TextView returnedText; 
private ToggleButton toggleButton; 
private ProgressBar progressBar; 
private SpeechRecognizer speech ; 
private Intent recognizerIntent; 
private String LOG_TAG = "MainActivity"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    returnedText = (TextView) findViewById(R.id.textView1); 
    progressBar = (ProgressBar) findViewById(R.id.progressBar1); 
    toggleButton = (ToggleButton) findViewById(R.id.toggleButton1); 

    progressBar.setVisibility(View.INVISIBLE); 

    initSpeech(); 

    toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(CompoundButton buttonView, 
            boolean isChecked) { 
      if (isChecked) { 
       progressBar.setVisibility(View.VISIBLE); 
       progressBar.setIndeterminate(true); 
       speech.startListening(recognizerIntent); 
      } else { 
       progressBar.setIndeterminate(false); 
       progressBar.setVisibility(View.INVISIBLE); 
       speech.stopListening(); 
      } 
     } 
    }); 

} 

private void initSpeech(){ 
    speech = SpeechRecognizer.createSpeechRecognizer(this); 
    speech.setRecognitionListener(this); 
    recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,"en"); 
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName()); 
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true); 
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1); 
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE, true); 
    recognizerIntent.putExtra("android.speech.extra.DICTATION_MODE", true); 
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 5000); 
} 

@Override 
public void onResume() { 
    super.onResume(); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    if (speech != null) { 
     speech.destroy(); 
     Log.i(LOG_TAG, "destroy"); 
    } 

} 

@Override 
public void onBeginningOfSpeech() { 
    Log.i(LOG_TAG, "onBeginningOfSpeech"); 
    progressBar.setIndeterminate(false); 
    progressBar.setMax(10); 
} 

@Override 
public void onBufferReceived(byte[] buffer) { 
    Log.i(LOG_TAG, "onBufferReceived: " + buffer); 
} 

@Override 
public void onEndOfSpeech() { 
    Log.i(LOG_TAG, "onEndOfSpeech"); 
    progressBar.setIndeterminate(true); 
    toggleButton.setChecked(false); 
} 

@Override 
public void onError(int errorCode) { 
    String errorMessage = getErrorText(errorCode); 
    Log.d(LOG_TAG, "FAILED " + errorMessage); 
    returnedText.setText(errorMessage); 
    toggleButton.setChecked(false); 
} 

@Override 
public void onEvent(int arg0, Bundle arg1) { 
    Log.i(LOG_TAG, "onEvent"); 
} 

@Override 
public void onPartialResults(Bundle partialResults) { 
    ArrayList<String> matches = partialResults 
      .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
    String text = ""; 
    for (String result : matches) 
     text += result + "\n"; 

    returnedText.append(text); 
} 

@Override 
public void onReadyForSpeech(Bundle arg0) { 
    Log.i(LOG_TAG, "onReadyForSpeech"); 
} 

@Override 
public void onResults(Bundle results) { 
    Log.i(LOG_TAG, "onResults"); 
    ArrayList<String> matches = results 
      .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
    String text = ""; 
    for (String result : matches) 
     text += result + "\n"; 

    returnedText.append(text); 
} 

@Override 
public void onRmsChanged(float rmsdB) { 
    Log.i(LOG_TAG, "onRmsChanged: " + rmsdB); 
    progressBar.setProgress((int) rmsdB); 
} 

public static String getErrorText(int errorCode) { 
    String message; 
    switch (errorCode) { 
     case SpeechRecognizer.ERROR_AUDIO: 
      message = "Audio recording error"; 
      break; 
     case SpeechRecognizer.ERROR_CLIENT: 
      message = "Client side error"; 
      break; 
     case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS: 
      message = "Insufficient permissions"; 
      break; 
     case SpeechRecognizer.ERROR_NETWORK: 
      message = "Network error"; 
      break; 
     case SpeechRecognizer.ERROR_NETWORK_TIMEOUT: 
      message = "Network timeout"; 
      break; 
     case SpeechRecognizer.ERROR_NO_MATCH: 
      message = "No match"; 
      break; 
     case SpeechRecognizer.ERROR_RECOGNIZER_BUSY: 
      message = "RecognitionService busy"; 
      break; 
     case SpeechRecognizer.ERROR_SERVER: 
      message = "error from server"; 
      break; 
     case SpeechRecognizer.ERROR_SPEECH_TIMEOUT: 
      message = "No speech input"; 
      break; 
     default: 
      message = "Didn't understand, please try again."; 
      break; 
    } 
    return message; 
} 
} 
+0

尝试从的onPause()方法去除super.onPause()。 –

+0

感谢您的建议,但它不起作用。当我完成识别语音并点击主页按钮时发生错误,但在首先初始化应用程序,然后单击主页按钮,没有发生错误。 –

回答

0
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

    @Override 
    public void onCheckedChanged(CompoundButton buttonView, 
           boolean isChecked) { 


     initSpeech(); 
     if (isChecked) { 
      progressBar.setVisibility(View.VISIBLE); 
      progressBar.setIndeterminate(true); 
      speech.startListening(recognizerIntent); 
     } else { 
      progressBar.setIndeterminate(false); 
      progressBar.setVisibility(View.INVISIBLE); 
      speech.stopListening(); 
     } 
    } 
}); 

的onCheckedChange()的内部呼叫initSpeech否则有时所述speechReconginzer对象可能被破坏。

这可能会帮助你

试图摧毁讲话对象的onError()endOfSpeech()和onResults()如果Reconginzer忙中出错发生

+0

感谢您的评论,它的工作原理。还有一件事。如果用户在切换按钮时语音识别器会在用户意外初始化或尝试按Home按钮时再次出现错误,那么如何防止这种情况呢?或者单击主页按钮时如何停止或销毁识别器?谢谢 –

+0

检查语音对象是否为空或不能为starListening和stopListening。并且检查是否与上面提到的问题相同 –

+0

是的先生,同样的错误,正如我上面提到的。这个时候发生错误,当开关按钮打开,然后当我点击主页按钮,发生错误..但是当切换按钮关闭,然后单击主页按钮它效果很好。 –