title: Getting text to speech activity to work at the same time as an onclick go to a new xml
body: <p>I am working on an app and I want the app to use text to speech to tell the user what button they have just pressed. I am having problems getting my button to work properly with the two conflicting activities. I am able to make it work either doing only text to speech or only when pressing it goes to a different page, but I can't get it to do both at the same time. Currently it sends the user to the new menu without playing the text to speech but I am finally getting an error from my LogCat so I figured it is fixable. I have included my LogCat and the java. I have commented above and below where the problem code is.</p>
  
  <pre><code>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.&lt;init&gt;(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.&lt;init&gt;(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)
  </code></pre>
  
  <p><strong>menu.java</strong></p>
  
  <pre><code>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();
          }
      }
  
  }
  </code></pre>