我在活动上有一个文字转语音按钮。所说的字符串通过intent传递给侦听器服务类。我最近刚刚将super.onStart添加到服务的onStart方法中,但是Eclipse告诉我这个已被弃用,所以我想知道这是否是我需要做的改变。这是代码:
public class Listener extends Service implements TextToSpeech.OnInitListener {
private String str;
private TextToSpeech mTts;
private static final String TAG = "TTSService";
private ArrayList<Button> b;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
mTts = new TextToSpeech(this, this // OnInitListener
);
mTts.setSpeechRate(0.5f);
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
if (mTts != null) {
mTts.stop();
mTts.shutdown();
Log.v(TAG, "ondestroy_service");
}
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
str = intent.getStringExtra("convo");
sayHello(str);
super.onStart(intent, startId);
}
@Override
public void onInit(int status) {
Log.v(TAG, "oninit");
mTts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String utteranceID) {
for (int i = 0; i < ChooseWord.buttonArrayNoClick.size(); i++) {
ChooseWord.buttonArrayNoClick.get(i).setClickable(false);
}
}
@Override
public void onError(String utteranceID) {
}
@Override
public void onDone(String utteranceID) {
for (int i = 0; i < ChooseWord.buttonArrayNoClick.size(); i++) {
ChooseWord.buttonArrayNoClick.get(i).setClickable(true);
}
}
});
if (status == TextToSpeech.SUCCESS) {
int result = mTts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.v(TAG, "Language is not available.");
} else {
sayHello(str);
}
} else {
Log.v(TAG, "Could not initialize TextToSpeech.");
}
}
private void sayHello(String str) {
mTts.speak(str, TextToSpeech.QUEUE_FLUSH, null);
}
}
这是错误报告:
java.lang.RuntimeException: Unable to start service com.mresapp.Listener@42060920 with null:
java.lang.NullPointerException
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2782)
at android.app.ActivityThread.access$2000(ActivityThread.java:152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1385)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.homeworkhelper.Listener.onStart(Unknown Source)
at android.app.Service.onStartCommand(Service.java:450)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2765)