J'ai eu cette erreur lorsque im appuyant sur le bouton d'accueil dans l'émulateur Androidandroid: Impossible de mettre en pause l'activité: java.lang.IllegalArgumentException: Service non enregistré:
java.lang.RuntimeException: Impossible de faire une pause activité {com .thesis.dic_ta_han/com.thesis.dic_ta_han.MainActivity}: java.lang.IllegalArgumentException: Service non enregistré: [email protected] at android.app.ActivityThread.performPauseActivity (ActivityThread.java:3260) at android.app.ActivityThread.performPauseActivity (ActivityThread.java:3219) at android.app.ActivityThread.handlePauseA ctivity (ActivityThread.java:3194) at android.app.ActivityThread.access $ 1000 (ActivityThread.java:151) à l'adresse android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1314) at android.os.Handler .dispatchMessage (Handler.java:102) à android.os.Looper.loop (Looper.java:135) à android.app.ActivityThread.main (ActivityThread.java:5254) à java.lang.reflect.Method .invoke (méthode native) à java.lang.reflect.Method.invoke (Method.java:372) à com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:903) à com. android.internal .os.ZygoteInit.main (ZygoteInit.java:698) Causé par: java.lang.IllegalArgumentException: Service non enregistré: [email protected] at android.app.LoadedApk.forgetServiceDispatcher (LoadedApk.java: 1029) à android.app.ContextImpl.unbindService (ContextImpl.java:1816) à android.content.ContextWrapper.unbindService (ContextWrapper.java:551) à android.speech.SpeechRecognizer.destroy (SpeechRecognizer.java:408) à com.thesis.dic_ta_han.MainActivity.onPause (MainActivity.java:83) à android.app.Activity.performPause (Activity.java:6101) at android.app.Instrumentat ion.callActivityOnPause (Instrumentation.java:1310) à android.app.ActivityThread.performPauseActivity (ActivityThread.java:3246) à android.app.ActivityThread.performPauseActivity (ActivityThread.java:3219) à android.app.ActivityThread. handlePauseActivity (ActivityThread.java:3194) à android.app.ActivityThread.access $ 1000 (ActivityThread.java:151) à android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1314) à android.os.Handler .dispatchMessage (Handler.java:102) à android.os.Looper.loop (Looper.java:135) à android.app.ActivityThread.main (ActivityThread.java:5254) à java.lang.reflect.Method.invoke (méthode native) à java.lang.reflect.Method.invoke (Method.java:372) à com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit .java: 903) à com.android.internal.os.ZygoteInit.main (ZygoteInit.java: 698)
Code de 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;
}
}
Essayez de supprimer super.onPause() du OnPause(). –
Merci pour votre suggestion, mais cela ne fonctionne pas. L'erreur s'est produite quand j'ai fini de reconnaître le discours et cliquez sur le bouton d'accueil, mais dans la première initialisation de l'application, puis en cliquant sur le bouton d'accueil aucune erreur ne s'est produite. –