我在我的项目中使用文本到语音但是它抛出了这个错误。 任何人都可以告诉我必须做什么,以便它有效...
代码....
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() {
String phoneNumber = null;
Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
String _ID = ContactsContract.Contacts._ID;
String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(CONTENT_URI, null, null, null,
null);
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String contact_id = cursor
.getString(cursor.getColumnIndex(_ID));
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor
.getColumnIndex(HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
Cursor phoneCursor = contentResolver.query(
PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?",
new String[] { contact_id }, null);
while (phoneCursor.moveToNext()) {
phoneNumber = phoneCursor.getString(phoneCursor
.getColumnIndex(NUMBER));
boolean a = PhoneNumberUtils.compare(number,
phoneNumber);
if (a) {
String contname = phoneCursor.getString(phoneCursor
.getColumnIndex(DISPLAY_NAME));
if (!contname.equals(null)) {
Log.i("Contact Name: ", contname);
Toast.makeText(getApplicationContext(),
contname, Toast.LENGTH_SHORT).show();
tts.speak(contname, TextToSpeech.QUEUE_FLUSH,
null);
}
}
}
phoneCursor.close();
}
}
}
}
}
Logcat是......
04-07 12:26:59.998: I/State(5948): Call is Ringing
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()
04-07 12:27:09.708: I/State(5948): Idle State`