大家好我在我的项目中使用texttospeech并且我在活动类中尝试了这个代码它工作正常但是在服务类中它工作不正常 我在我的项目中使用文本到语音,但它抛出了这个错误。任何人都可以告诉我必须做什么,以便它有效...
代码....
public class CallerService extends Service implements
TextToSpeech.OnInitListener {
String number;
private TextToSpeech tts;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
tts = new TextToSpeech(this, this);
}
@Override
@Deprecated
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
number = intent.getStringExtra("phnumber");
fetchContacts();
}
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status != TextToSpeech.ERROR)
{
tts.setLanguage(Locale.UK);
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
public void fetchContacts() {
tts.speak("kd arjit", TextToSpeech.QUEUE_FLUSH,
null);}
}
}
Logcat是......
04-07 12:27:00.028: I/TextToSpeech.java(5948): initTts() successfully bound to service
04-07 12:27:01.038: D/dalvikvm(5948): GC_EXPLICIT freed 83K, 45% free 3123K/5639K, `external 743K/1036K, paused 54ms
04-07 12:27:02.158: I/Contact Name:(5948): kd arjit
04-07 12:27:02.168: I/TextToSpeech.java - speak(5948): speak text of length 8
04-07 12:27:02.168: E/TextToSpeech.java - speak(5948): service isn't started
04-07 12:27:02.298: D/dalvikvm(5948): GC_CONCURRENT freed 375K, 48% free 3142K/5959K, external 758K/1036K, paused 2ms+4ms
04-07 12:27:02.308: W/CursorWrapperInner(5948): Cursor finalized without prior close()
答案 0 :(得分:0)
您的logcat并未真正显示任何错误,但问题是您致电:
fetchContacts();
之前:
public void onInit(int status)
成功执行。只有在成功执行tts.speak
回调后,您才能调用onInit
。因此,您应该将fetchContacts();
移到onInit
内。