这段代码似乎有问题,每次它说我没有在创建时安装texttoSpeech而且我已经完成了,当我点击开始阅读按钮时应用程序崩溃没有错误消息...所以继承人代码..
public class CBrecipeReader extends Activity{
public Cursor cursor;
CBDataBaseHelper data;
public static int ReadData = 1;
public TextToSpeech Speak = null;
public boolean SpeakInit = false;
public TextView Speech;
public TextView rowIDText2;
CBListAdapter adapter;
String RowID;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.texttospeech);
Bundle extras = getIntent().getExtras();
Button StartSpeech = (Button)findViewById(R.id.startSpeech);
rowIDText2 = (TextView)findViewById(R.id.rowID2);
Speech = (TextView) findViewById(R.id.sayThis);
if (extras != null) {
RowID = extras.getString("SELECTED3");
rowIDText2.setText(RowID);
}
if (rowIDText2.getText() != ""){
data = new CBDataBaseHelper(this);
String s = rowIDText2.getText().toString();
int ID = Integer.parseInt(s);
data.open();
Cursor cursor = data.fetchRow(ID);
if (cursor.moveToFirst()){ // data?
Speech.setText(cursor.getString(1) + " " + cursor.getString(2) + " " + cursor.getString(3));
}
data.close();
}
//+ " " + cursor.getString(2) + "" + cursor.getString(3)
initSpeak();
speakButtons(StartSpeech);
}
protected void initSpeak(){
Intent intent = new Intent(Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(intent, ReadData);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == ReadData){
if (resultCode == Engine.CHECK_VOICE_DATA_PASS){
Speak = new TextToSpeech(this, new OnInitListener() {
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS){
SpeakInit = true;
if(Speak.isLanguageAvailable(Locale.UK) >= 0){
Speak.setLanguage(Locale.UK);
Speak.setPitch(0.8f);
Speak.setSpeechRate(1.1f);
}
}
}
});
}else {
Intent installVoice = new Intent(Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installVoice);
}
}
}
protected void speakButtons(View view){
boolean diditwork;
try {
Speech = (TextView) findViewById(R.id.sayThis);
if (Speak != null && SpeakInit){
Speak.speak("test", TextToSpeech.QUEUE_ADD, null);
} else if (SpeakInit == false){
Toast.makeText(getApplicationContext(), "No Text to speech Installed ", Toast.LENGTH_SHORT).show();
}
}catch(Exception e){
diditwork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("darn");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}
}
}
答案 0 :(得分:0)
您的代码与我的代码非常相似,我能看到的唯一区别是
我打电话:
Speak.speak("test", TextToSpeech.QUEUE_FLUSH, null);
你打电话:
Speak.speak("test", TextToSpeech.QUEUE_ADD, null);