Android RecyclerView中的TextToSpeech:错误:android.app.ServiceConnectionLeaked

时间:2017-11-15 11:20:00

标签: android android-recyclerview text-to-speech android-viewholder

我在RecyclerView中实现了TextToSpeech。每个卡片项都有一个初始化说话的按钮。我已将OnClickListener设置为MyViewHolder中的按钮。 MyViewHolder实现了TextToSpeech.OnInitListener。该应用程序运行正常。

但是,当活动结束时,我必须关闭并停止TextToSpeech 。由于我无法覆盖MyViewHolder类中的onDestroy()方法,我该怎么办呢?

public class MyViewHolder extends RecyclerView.ViewHolder implements TextToSpeech.OnInitListener {

private Button btnSpeak;
private TextToSpeech textToSpeech;

public MyViewHolder(final View itemView) {
        super(itemView);

        textToSpeech = new TextToSpeech(itemView.getContext(), this);
        btnSpeak= (Button) itemView.findViewById(R.id.btn_speak);
        btnSpeak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textToSpeech.speak(wordToSpeak, TextToSpeech.QUEUE_FLUSH, null);
            }
        });
}

@Override
public void onInit(int status) {

        if (status == TextToSpeech.SUCCESS) {

            int result = textToSpeech.setLanguage(Locale.US);

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            }

        } else {
            Log.e("TTS", "Initilization Failed!");
        }
    }
}

每个卡片项的日志显示以下错误:

android.app.ServiceConnectionLeaked: Activity has leaked ServiceConnection android.speech.tts.TextToSpeech that was originally bound here.

0 个答案:

没有答案