我正在开发一个应用程序,并且希望该应用程序使用文本语音转换来告诉用户他们刚刚按下了什么按钮。我在使按钮无法正常使用这两个相互冲突的活动时遇到问题。我能够使它仅工作于文本到语音,或者仅在按下它进入另一页时才起作用,但是我不能同时使它们同时工作。目前,它会将用户发送到新菜单,而没有播放文本到语音,但是我最终从LogCat遇到错误,因此我认为它是可以修复的。我包括了LogCat和Java。我在上面和下面评论了问题代码在哪里。

07-25 12:52:35.031: E/ActivityThread(328): Activity com.example.com.proto1.menu has leaked ServiceConnection android.speech.tts.TextToSpeech$1@4051eb10 that was originally bound here
07-25 12:52:35.031: E/ActivityThread(328): android.app.ServiceConnectionLeaked: Activity com.example.com.proto1.menu has leaked ServiceConnection android.speech.tts.TextToSpeech$1@4051eb10 that was originally bound here
07-25 12:52:35.031: E/ActivityThread(328):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:938)
07-25 12:52:35.031: E/ActivityThread(328):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:833)
07-25 12:52:35.031: E/ActivityThread(328):  at android.app.ContextImpl.bindService(ContextImpl.java:867)
07-25 12:52:35.031: E/ActivityThread(328):  at android.content.ContextWrapper.bindService(ContextWrapper.java:347)
07-25 12:52:35.031: E/ActivityThread(328):  at android.speech.tts.TextToSpeech.initTts(TextToSpeech.java:467)
07-25 12:52:35.031: E/ActivityThread(328):  at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:433)
07-25 12:52:35.031: E/ActivityThread(328):  at com.example.com.proto1.menu.onActivityResult(menu.java:122)
07-25 12:52:35.031: E/ActivityThread(328):  at android.app.Activity.dispatchActivityResult(Activity.java:3908)
07-25 12:52:35.031: E/ActivityThread(328):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
07-25 12:52:35.031: E/ActivityThread(328):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
07-25 12:52:35.031: E/ActivityThread(328):  at android.app.ActivityThread.access$2000(ActivityThread.java:117)
07-25 12:52:35.031: E/ActivityThread(328):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
07-25 12:52:35.031: E/ActivityThread(328):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 12:52:35.031: E/ActivityThread(328):  at android.os.Looper.loop(Looper.java:123)
07-25 12:52:35.031: E/ActivityThread(328):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-25 12:52:35.031: E/ActivityThread(328):  at java.lang.reflect.Method.invokeNative(Native Method)
07-25 12:52:35.031: E/ActivityThread(328):  at java.lang.reflect.Method.invoke(Method.java:507)
07-25 12:52:35.031: E/ActivityThread(328):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-25 12:52:35.031: E/ActivityThread(328):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-25 12:52:35.031: E/ActivityThread(328):  at dalvik.system.NativeStart.main(Native Method)


menu.java

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.speech.RecognizerIntent;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.speech.tts.TextToSpeech;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@SuppressWarnings("unused")
public class menu extends Activity implements TextToSpeech.OnInitListener,
        OnClickListener {

    TextToSpeech mTts;
    Button speakButton;

    // TTS object
    public TextToSpeech myTTS;
    // status check code
    public int MY_DATA_CHECK_CODE = 0;

    @Override
    protected void onCreate(Bundle aboutmenu) {
        // TODO Auto-generated method stub
        super.onCreate(aboutmenu);
        setContentView(R.layout.mainx);

        SpeakingAndroid speak = new SpeakingAndroid();

        // get a reference to the button element listed in the XML layout
        speakButton = (Button) findViewById(R.id.btn_speak);
        // listen for clicks
        speakButton.setOnClickListener(this);

        // check for TTS data
        Intent checkTTSIntent = new Intent();
        checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);

        // Setting up the button references
        Button info = (Button) findViewById(R.id.aboutbutton);
        Button voice = (Button) findViewById(R.id.voicebutton);

        info.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent("android.intent.action.INFOSCREEN"));

            }
        });

        voice.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                try {

                } catch (Exception e) {

                }
            }
        });
// This is the start of the problem area
        starteyephone();
        {
            speakButton.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent voiceIntent = new Intent(
                            "android.intent.action.RECOGNITIONMENU");
                    startActivity(voiceIntent);

                }
            });
        }

    }


    private void starteyephone() {
        // TODO Auto-generated method stub

    }
    // respond to button clicks
    public void onClick(View v) {

        // get the text entered
        speakButton = (Button) findViewById(R.id.btn_speak);
        String words = speakButton.getText().toString();
        speakWords(words);
    }

    //this is the end of the problem area


    // speak the user text
    public void speakWords(String speech) {

        // speak straight away
        myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
    }

    // act on result of TTS data check
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == MY_DATA_CHECK_CODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                // the user has the necessary data - create the TTS
                myTTS = new TextToSpeech(this, this);
            } else {
                // no data - install it now
                Intent installTTSIntent = new Intent();
                installTTSIntent
                        .setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installTTSIntent);
            }
        }
    }

    // setup TTS
    public void onInit(int initStatus) {

        // check for successful instantiation
        if (initStatus == TextToSpeech.SUCCESS) {
            if (myTTS.isLanguageAvailable(Locale.US) == TextToSpeech.LANG_AVAILABLE)
                myTTS.setLanguage(Locale.US);
        } else if (initStatus == TextToSpeech.ERROR) {
            Toast.makeText(this, "Sorry! Text To Speech failed...",
                    Toast.LENGTH_LONG).show();
        }
    }

}

最佳答案

我在这里看到许多问题:


您可以使用菜单活动的上下文初始化TextToSpeech。这意味着当菜单活动被销毁时,TextToSpeech实例将被泄漏,因为它从未被正确关闭(这可能就是您在logcat中看到该错误的原因)。您需要在myTTS.shutdown()中调用menu.onDestroy()或在此处初始化getApplicationContext()时需要传递TextToSpeech作为上下文参数:

myTTS = new TextToSpeech(getApplicationContext(),this);

注意:如果使用应用程序上下文,则需要确定何时不再需要TTS,然后在该时刻调用shutdown()
您正在使用android.intent.action.RECOGNITIONMENU,但是此Intent用于语音识别(即:语音到文本),而不是文本到语音。我不明白您要在这里做什么。
我仍然不明白您的确切问题。在我看来,您发布的代码应该说出按钮的文字。您还期望在这里发生什么?


编辑:添加观察

现在,我看到您在setOnClickListener()上两次调用speakButton。在onCreate()的开头,然后在“问题部分”中,再次。您不能设置多个onClickListener,因此第一个被覆盖。您应该只移动以下代码:

Intent voiceIntent = new Intent("android.intent.action.RECOGNITIONMENU");
startActivity(voiceIntent);


到现有的onClick()方法中。

10-07 19:43
查看更多