我正在使用Android Studio开发一个Android应用程序,其中该应用程序会告诉我一些我曾经插入的单词(此操作没问题)。当我从设备“任务管理器”关闭应用程序时,一切正常,但是如果我按设备上的“后退”按钮,则该应用程序会显示以下错误:
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");
}
}
});
}
我真的不知道为什么会给我这个错误,希望您能帮助我。
答案 0 :(得分:1)
您可以这样添加:
@Override
public void onBackPressed() {
textToSpeech.stop();
textToSpeech.shutdown();
myTimer.cancel();
super.onBackPressed();
}