我正在创建一个涉及文本消息的 android 应用程序,其中一个功能是在输入时读出每个字母。我正在使用TextToSpeech。
我让它在一个测试项目中工作,但是当将它集成到我自己的项目中时,我收到了一个未知的异常。
谁能看到可能导致这种情况的原因并提出补救措施?我创建了一个名为speech 的类,它在另一个类中实例化,并且有一个调用speak out 的方法(类似于下面的第二个示例。)还有谁能解释为什么我需要在第一个示例中扩展Activity?同样有问题的行说"new TextToSpeech is not defined"。
片段在哪里不起作用。错误开启tts = new TextToSpeech(this, this);
public class Speech extends Activity implements TextToSpeech.OnInitListener {
private TextToSpeech tts;
private String toRead;
public Speech(String toRead){
this.toRead = toRead;
tts = new TextToSpeech(this, this);
}
这是它在哪里工作的代码
public class MainActivity extends Activity implements
TextToSpeech.OnInitListener {
/** Called when the activity is first created. */
private TextToSpeech tts;
private Button btnSpeak;
private EditText txtText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(this, this);
btnSpeak = (Button) findViewById(R.id.btnSendSMS);
txtText = (EditText) findViewById(R.id.txtMessage);
// button on click event
btnSpeak.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
speakOut();
}
});
}
@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");
} else {
btnSpeak.setEnabled(true);
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakOut() {
String text = txtText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
日志信息
02-13 20:00:54.974: D/AndroidRuntime(11127): Shutting down VM
02-13 20:00:54.974: W/dalvikvm(11127): threadid=1: thread exiting with uncaught exception (group=0x411cd300)
02-13 20:00:54.979: E/AndroidRuntime(11127): FATAL EXCEPTION: main
02-13 20:00:54.979: E/AndroidRuntime(11127): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.BT/org.BT.Text_entry}: java.lang.NullPointerException
02-13 20:00:54.979: E/AndroidRuntime(11127): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
02-13 20:00:54.979: E/AndroidRuntime(11127): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
02-13 20:00:54.979: E/AndroidRuntime(11127): at android.app.ActivityThread.access$600(ActivityThread.java:140)
02-13 20:00:54.979: E/AndroidRuntime(11127): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
02-13 20:00:54.979: E/AndroidRuntime(11127): at android.os.Handler.dispatchMessage(Handler.java:99)
02-13 20:00:54.979: E/AndroidRuntime(11127): at android.os.Looper.loop(Looper.java:137)
02-13 20:00:54.979: E/AndroidRuntime(11127): at android.app.ActivityThread.main(ActivityThread.java:4898)
02-13 20:00:54.979: E/AndroidRuntime(11127): at java.lang.reflect.Method.invokeNative(Native Method)
02-13 20:00:54.979: E/AndroidRuntime(11127): at java.lang.reflect.Method.invoke(Method.java:511)
02-13 20:00:54.979: E/AndroidRuntime(11127): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-13 20:00:54.979: E/AndroidRuntime(11127): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-13 20:00:54.979: E/AndroidRuntime(11127): at dalvik.system.NativeStart.main(Native Method)
02-13 20:00:54.979: E/AndroidRuntime(11127): Caused by: java.lang.NullPointerException
02-13 20:00:54.979: E/AndroidRuntime(11127): at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:91)
02-13 20:00:54.979: E/AndroidRuntime(11127): at android.speech.tts.TtsEngines.getDefaultEngine(TtsEngines.java:75)
02-13 20:00:54.979: E/AndroidRuntime(11127): at android.speech.tts.TextToSpeech.getDefaultEngine(TextToSpeech.java:1235)
02-13 20:00:54.979: E/AndroidRuntime(11127): at android.speech.tts.TextToSpeech.initTts(TextToSpeech.java:595)
02-13 20:00:54.979: E/AndroidRuntime(11127): at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:553)
02-13 20:00:54.979: E/AndroidRuntime(11127): at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:527)
02-13 20:00:54.979: E/AndroidRuntime(11127): at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:512)
02-13 20:00:54.979: E/AndroidRuntime(11127): at org.BT.Speech.<init>(Speech.java:17)
02-13 20:00:54.979: E/AndroidRuntime(11127): at org.BT.Text_entry.<init>(Text_entry.java:48)
02-13 20:00:54.979: E/AndroidRuntime(11127): at java.lang.Class.newInstanceImpl(Native Method)
02-13 20:00:54.979: E/AndroidRuntime(11127): at java.lang.Class.newInstance(Class.java:1319)
02-13 20:00:54.979: E/AndroidRuntime(11127): at android.app.Instrumentation.newActivity(Instrumentation.java:1057)
02-13 20:00:54.979: E/AndroidRuntime(11127): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)
02-13 20:00:54.979: E/AndroidRuntime(11127): ... 11 more
- - - - - - - - - - - - - - - - - - - - -编辑 - - - - ----------------------------------------------------------- 进一步的研究有建议可能是因为我在 android 上下文之外尝试 Android 框架?这可能是真的吗 - 因为我使用的是课程而不是活动?如果是这样,我将如何解决这个问题?