gpt4 book ai didi

java - TextToSpeech 抛出未知异常

转载 作者:行者123 更新时间:2023-12-01 14:50:57 28 4
gpt4 key购买 nike

我正在创建一个涉及短信的 Android 应用程序,其中一个功能是输入时会读出每个字母。我正在使用TextToSpeech

我让它在一个测试项目中工作,但是当将它集成到我自己的项目中时,我收到一个未知异常

任何人都可以看到可能导致此问题的原因并提出补救措施吗?我创建了一个名为“speech”的类,它在另一个类中实例化,并且有一个调用“speak out”的方法(类似于下面的第二个示例。)还有人可以解释为什么我需要在第一个示例上扩展 Activity 吗?同样有问题的行表示“未定义新的 TextToSpeech”

fragment 不起作用。错误发生在 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);
}
}

Logcat信息

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 框架?这是真的吗?因为我使用的是类(class)而不是 Activity ?如果是这样我该如何解决这个问题?

最佳答案

在我看来,这是 TextToSpeech (TTS) 的标准示例,可以在互联网上随处找到,并且它应该可以毫无问题地工作。 (但是,这并不意味着此代码示例不存在任何设计问题。)

也许您使用的 Android 版本太旧,或者您的手机上尚未正确设置 TTS?您检查过手机上的 TTS 设置吗? (通常情况下,如果您的手机上未正确设置 TTS,此代码应调用正确的安装页面来执行此操作,但我记得看到一条消息,该部分有时会崩溃。)

您还应该检查 txtText 在使用 findViewById() 初始化后是否不为 null。

关于java - TextToSpeech 抛出未知异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14861707/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com