-1
下面是我有:Android的文本到语音的NullPointerException
ImageView iv, in, out;
Button back, home, talk;
TextView header, desc;
private TextToSpeech tts;
private void initControls(){
iv = (ImageView) findViewById (R.id.ivExercise);
back = (Button) findViewById (R.id.tabs_back);
home = (Button) findViewById (R.id.tabs_home);
back.setOnClickListener(this);
home.setOnClickListener(this);
header = (TextView) findViewById (R.id.txtMenuHeader);
header.setText(R.string.bicycling);
talk = (Button) findViewById (R.id.btnTalk);
desc = (TextView) findViewById (R.id.notedesc);
desc.setText(R.string.bicyclingnote);
iv.setImageResource(R.drawable.bicycling);
out = (ImageView) findViewById (R.id.ibZoomOut);
in = (ImageView) findViewById (R.id.ibZoomIn);
out.setOnClickListener(this);
in.setOnClickListener(this);
talk.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.tabs_back:
finish();
break;
case R.id.tabs_home:
Intent home = new Intent(Bicycling.this, IHealthFirstActivity.class);
home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(home);
break;
case R.id.ibZoomIn:
desc.setTextSize(TypedValue.COMPLEX_UNIT_PX,(desc.getTextSize()+1f));
break;
case R.id.ibZoomOut:
desc.setTextSize(TypedValue.COMPLEX_UNIT_PX,(desc.getTextSize()-1f));
break;
case R.id.btnTalk:
speakOut();
break;
}
}
@Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
// Log.e("TTS", "This Language is not supported");
Toast.makeText(getBaseContext(), "This language is not supported", Toast.LENGTH_SHORT).show();
} else {
talk.setEnabled(true);
speakOut();
}
} else {
// Log.e("TTS", "Initilization Failed!");
Toast.makeText(getBaseContext(), "Initialization Failed!", Toast.LENGTH_SHORT).show();
}
}
private void speakOut() {
// TODO Auto-generated method stub
String text = "I'm Android!"; //desc.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
我在这一行一个NullPointerException:
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
我跟经过这里的教程http://www.androidhive.info/2012/01/android-text-to-speech-tutorial/ 我想有什么东西我失踪。请帮忙。谢谢。
TTS未初始化 –