У меня проблема при нажатии кнопки «назад» на моем телефоне - PullRequest
0 голосов
/ 16 марта 2020

Я делаю приложение Android со Android studio, где приложение говорит некоторые слова, которые я вставил ранее (с этим проблем нет). Когда я закрываю приложение с устройства «диспетчер задач», все работает нормально, но если я нажимаю кнопку «назад» на моем устройстве, приложение выдает мне эту ошибку:

2020-03-16 17:12:23.702 25085-25085/com.example.chip E/ActivityThread: Activity com.example.chip.Activity_punteggio has leaked ServiceConnection android.speech.tts.TextToSpeech$Connection@faa87c1 that was originally bound here
android.app.ServiceConnectionLeaked: Activity com.example.chip.Activity_punteggio has leaked ServiceConnection android.speech.tts.TextToSpeech$Connection@faa87c1 that was originally bound here
    at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:1804)
    at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:1695)
    at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1892)
    at android.app.ContextImpl.bindService(ContextImpl.java:1845)
    at android.content.ContextWrapper.bindService(ContextWrapper.java:698)
    at android.speech.tts.TextToSpeech.connectToEngine(TextToSpeech.java:817)
    at android.speech.tts.TextToSpeech.initTts(TextToSpeech.java:787)
    at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:740)
    at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:719)
    at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:703)
    at com.example.chip.Activity_punteggio.startVoice(Activity_punteggio.java:347)
    at com.example.chip.Activity_punteggio$6.onClick(Activity_punteggio.java:237)
    at android.view.View.performClick(View.java:6663)
    at android.view.View.performClickInternal(View.java:6635)
    at android.view.View.access$3100(View.java:794)
    at android.view.View$PerformClick.run(View.java:26199)
    at android.os.Handler.handleCallback(Handler.java:907)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:216)
    at android.app.ActivityThread.main(ActivityThread.java:7625)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)

Я также помещаю некоторый код здесь, чтобы вы могли лучше понять:

public void startVoice(){
    voiceOn = true;
    textToSpeech = new TextToSpeech(Activity_punteggio.this, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status == TextToSpeech.SUCCESS) {
                int result = textToSpeech.setLanguage(Locale.ITALIAN);
                if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    Log.e("TTS", "Language not supported");
                } else {
                    myTimer = new Timer();
                    myTask = new TimerTask() {
                        @Override
                        public void run() {
                            speak();
                        }
                    };
                    myTimer.schedule(myTask, 1000, FREQUENCY * (60 * 10));
                }
            } else {
                Log.e("TTS", "Initialization failed");
            }
        }
    });
}

Я действительно не знаю, почему это дает мне эту ошибку, я надеюсь, что вы могли бы помочь мне.

1 Ответ

1 голос
/ 16 марта 2020

Вы можете добавить вот так:

@Override
public void onBackPressed() {
    textToSpeech.stop();
    textToSpeech.shutdown();
    myTimer.cancel();
    super.onBackPressed();
}
...