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