Android:无法暂停活动:java.lang.IllegalArgumentException:服务未注册:

问题描述 投票:3回答:1

我在Android Emulator中按下Home按钮时出现此错误。

java.lang.RuntimeException: Unable to pause activity {com.thesis.dic_ta_han/com.thesis.dic_ta_han.MainActivity}: 
java.lang.IllegalArgumentException: Service not registered: android.speech.SpeechRecognizer$Connection@170be3d7 
    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3260) 
    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3219) 
    at android.app.ActivityThread.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) 
    at java.lang.reflect.Method.invoke(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) Caused by: 
java.lang.IllegalArgumentException: Service not registered: android.speech.SpeechRecognizer$Connection@170be3d7 
    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.Instrumentation.callActivityOnPause(Instrumentation.java:1310) 
    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3246) 
    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3219)  
    at android.app.ActivityThread.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)  
    at java.lang.reflect.Method.invoke(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)  

我的名字是c:

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;
}
}
android illegalargumentexception
1个回答
1
投票
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对象。

这可能对你有所帮助

如果发生Reconginzer忙错误,尝试销毁语句对象onError()endOfSpeech()和onResults()

© www.soinside.com 2019 - 2024. All rights reserved.