所以我第一次尝试将文本语音转换为我的应用程序。在讲话时我仍然会想到错误。这是我得到的日志:
01-23 18:52:29.825: E/AndroidRuntime(24380): FATAL EXCEPTION: main
01-23 18:52:29.825: E/AndroidRuntime(24380): Process: com.rcd.league, PID: 24380
01-23 18:52:29.825: E/AndroidRuntime(24380): java.lang.NullPointerException
01-23 18:52:29.825: E/AndroidRuntime(24380): at com.rcd.league.speakActivity.speakOut(speakActivity.java:60)
01-23 18:52:29.825: E/AndroidRuntime(24380): at com.rcd.league.spawnTimers$15.onClick(Timers.java:350)
01-23 18:52:29.825: E/AndroidRuntime(24380): at android.view.View.performClick(View.java:4442)
01-23 18:52:29.825: E/AndroidRuntime(24380): at android.view.View$PerformClick.run(View.java:18423)
01-23 18:52:29.825: E/AndroidRuntime(24380): at android.os.Handler.handleCallback(Handler.java:733)
01-23 18:52:29.825: E/AndroidRuntime(24380): at android.os.Handler.dispatchMessage(Handler.java:95)
01-23 18:52:29.825: E/AndroidRuntime(24380): at android.os.Looper.loop(Looper.java:137)
01-23 18:52:29.825: E/AndroidRuntime(24380): at android.app.ActivityThread.main(ActivityThread.java:5083)
01-23 18:52:29.825: E/AndroidRuntime(24380): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 18:52:29.825: E/AndroidRuntime(24380): at java.lang.reflect.Method.invoke(Method.java:515)
01-23 18:52:29.825: E/AndroidRuntime(24380): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-23 18:52:29.825: E/AndroidRuntime(24380): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-23 18:52:29.825: E/AndroidRuntime(24380): at dalvik.system.NativeStart.main(Native Method)
这是我的代码:
package com.rcd.league;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
public class speakActivity extends Activity implements TextToSpeech.OnInitListener {
private int MY_DATA_CHECK_CODE = 0;
private TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
tts = new TextToSpeech(this, this);
}
@Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
@Override
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");
} else {
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
public void speakOut() {
String text = "test";
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
我正在呼叫speakOut();来自另一项活动。以下是该代码的剪辑:
speakActivity speak = new speakActivity();
...
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (btTimer.getText().toString().contains(":50")){
cd[0].start();
speak.speakOut();
} else {
cd[0].cancel();
cd[0].onFinish();
}
}
});
任何帮助将不胜感激! 谢谢!
编辑:我也尝试了QUEUE_ADD而不是QUEUE_FLUSH,但也没有用:/
答案 0 :(得分:2)
您创建了speakActivity
活动,但在您发布的代码中,您从未启动过活动。由于活动从未启动过,因此从未调用onCreate()
,因此tts
未初始化。这就是您致电
NullPointerException
的原因
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
speakOut()
中的。
有关何时调用onCreate()
的详细信息,请参阅此stackoverflow问题:Is onCreate called when an Activity object is created?