1

我正在开发一个应用程序,我希望该应用程序使用文本到语音来告诉用户他们刚刚按下了哪个按钮。我无法让我的按钮在两个相互冲突的活动中正常工作。我可以让它工作,要么只做文本到语音,要么只在按下它时进入不同的页面,但我不能让它同时做这两个。目前它将用户发送到新菜单而不播放文本到语音,但我终于从我的 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)

菜单.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();
        }
    }

}
4

1 回答 1

2

我在这里看到了一些问题:

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

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

    注意:如果您使用应用程序上下文,则需要确定何时不再需要 TTS 并shutdown()在那时调用它。

  2. 您正在使用android.intent.action.RECOGNITIONMENU,但此 Intent 用于语音识别(即:语音到文本),而不是文本到语音。我不明白你想在这里做什么。

  3. 我仍然不明白你的确切问题。在我看来,您发布的代码应该说出按钮的文本。您还期望在这里发生什么?

编辑:添加观察

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

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

到您现有的onClick()方法中。

于 2012-07-25T13:51:56.807 回答