我在按 phone 上的 "back" 按钮时遇到问题

I got a problem when I'm pressing the "back" button on my phone

我正在用 Android studio 做一个 Android 应用程序,应用程序告诉我以前插入的一些词(这没问题)。当我从设备 "task manager" 关闭应用程序时一切正常,但如果我按下设备上的 "back" 按钮,应用程序会给我这个错误:

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.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.access00(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");
            }
        }
    });
}

我真的不知道为什么会给我这个错误,希望你能帮助我。

你可以这样添加:

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