无法从非活动课程中获得文本转语音工作
我尝试使用文本转语音,但出现空指针异常。我有许多带有文本初始化的文本视图。我尝试做的是,当用户单击此文本视图时,文本转语音会说出该特定内容上的文本textview.以下是我处理文本到语音的两个类的代码。我还标记了出现空指针异常的行。请告诉我我是什么做错事了?!谢谢。
我的 TextSpeaker 类:
public class TextSpeaker implements OnInitListener{
TextToSpeech tts;
public TextSpeaker(Context c,OnInitListener listener){
tts = new TextToSpeech(c,listener); //Getting NullPointerException here
}
@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR){
tts.setLanguage(Locale.US);
}
}
}
我的 TextViewClick 类:
public class TextViewClick implements OnClickListener{
public String textHolder;
Context c;
TextToSpeech.OnInitListener listener;
TextSpeaker tts1 = new TextSpeaker(c,listener); //Getting NullPointerException here
@Override
public void onClick(View v) {
TextView tv=(TextView) v;
tv.setTextColor(R.color.red);
textHolder = (String) tv.getText();
System.out.println(textHolder);
tts1.tts.speak(textHolder, TextToSpeech.QUEUE_FLUSH, null);
}
}
我的 logcat 消息:
01-18 13:59:20.017: E/AndroidRuntime(323): FATAL EXCEPTION: main
01-18 13:59:20.017: E/AndroidRuntime(323): java.lang.NullPointerException
01-18 13:59:20.017: E/AndroidRuntime(323): at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:407)
01-18 13:59:20.017: E/AndroidRuntime(323): at blah.blah.com.TextSpeaker.<init>(TextSpeaker.java:13)
01-18 13:59:20.017: E/AndroidRuntime(323): at independent.vervecoders.com.TextViewClick.<init>(TextViewClick.java:13)
01-18 13:59:20.017: E/AndroidRuntime(323): at independent.vervecoders.com.Independent.onClick(Independent.java:86)
01-18 13:59:20.017: E/AndroidRuntime(323): at android.view.View.performClick(View.java:2408)
01-18 13:59:20.017: E/AndroidRuntime(323): at android.view.View$PerformClick.run(View.java:8816)
01-18 13:59:20.017: E/AndroidRuntime(323): at android.os.Handler.handleCallback(Handler.java:587)
01-18 13:59:20.017: E/AndroidRuntime(323): at android.os.Handler.dispatchMessage(Handler.java:92)
01-18 13:59:20.017: E/AndroidRuntime(323): at android.os.Looper.loop(Looper.java:123)
01-18 13:59:20.017: E/AndroidRuntime(323): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-18 13:59:20.017: E/AndroidRuntime(323): at java.lang.reflect.Method.invokeNative(Native Method)
01-18 13:59:20.017: E/AndroidRuntime(323): at java.lang.reflect.Method.invoke(Method.java:521)
01-18 13:59:20.017: E/AndroidRuntime(323): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-18 13:59:20.017: E/AndroidRuntime(323): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-18 13:59:20.017: E/AndroidRuntime(323): at dalvik.system.NativeStart.main(Native Method)
I tried to use Text-to-Speech but getting an nullpointerexception.I have many textviews with text init.what i tried to do is that when user clicks on this textviews than text-to-speech speaks up the text which is on that particular textview.Following is the code of 2 of my classes which handles text-to-speech.I have also marked down the lines where i'm getting the nullpointerexception.Please tell me what am i doing wrong?! Thank you.
My TextSpeaker class:
public class TextSpeaker implements OnInitListener{
TextToSpeech tts;
public TextSpeaker(Context c,OnInitListener listener){
tts = new TextToSpeech(c,listener); //Getting NullPointerException here
}
@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR){
tts.setLanguage(Locale.US);
}
}
}
My TextViewClick class:
public class TextViewClick implements OnClickListener{
public String textHolder;
Context c;
TextToSpeech.OnInitListener listener;
TextSpeaker tts1 = new TextSpeaker(c,listener); //Getting NullPointerException here
@Override
public void onClick(View v) {
TextView tv=(TextView) v;
tv.setTextColor(R.color.red);
textHolder = (String) tv.getText();
System.out.println(textHolder);
tts1.tts.speak(textHolder, TextToSpeech.QUEUE_FLUSH, null);
}
}
My logcat messages:
01-18 13:59:20.017: E/AndroidRuntime(323): FATAL EXCEPTION: main
01-18 13:59:20.017: E/AndroidRuntime(323): java.lang.NullPointerException
01-18 13:59:20.017: E/AndroidRuntime(323): at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:407)
01-18 13:59:20.017: E/AndroidRuntime(323): at blah.blah.com.TextSpeaker.<init>(TextSpeaker.java:13)
01-18 13:59:20.017: E/AndroidRuntime(323): at independent.vervecoders.com.TextViewClick.<init>(TextViewClick.java:13)
01-18 13:59:20.017: E/AndroidRuntime(323): at independent.vervecoders.com.Independent.onClick(Independent.java:86)
01-18 13:59:20.017: E/AndroidRuntime(323): at android.view.View.performClick(View.java:2408)
01-18 13:59:20.017: E/AndroidRuntime(323): at android.view.View$PerformClick.run(View.java:8816)
01-18 13:59:20.017: E/AndroidRuntime(323): at android.os.Handler.handleCallback(Handler.java:587)
01-18 13:59:20.017: E/AndroidRuntime(323): at android.os.Handler.dispatchMessage(Handler.java:92)
01-18 13:59:20.017: E/AndroidRuntime(323): at android.os.Looper.loop(Looper.java:123)
01-18 13:59:20.017: E/AndroidRuntime(323): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-18 13:59:20.017: E/AndroidRuntime(323): at java.lang.reflect.Method.invokeNative(Native Method)
01-18 13:59:20.017: E/AndroidRuntime(323): at java.lang.reflect.Method.invoke(Method.java:521)
01-18 13:59:20.017: E/AndroidRuntime(323): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-18 13:59:20.017: E/AndroidRuntime(323): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-18 13:59:20.017: E/AndroidRuntime(323): at dalvik.system.NativeStart.main(Native Method)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您的上下文从未初始化。确保在调用 TextSpeaker 的构造函数之前初始化上下文。
您也不需要传递 onInitListener,您的 TextSpeaker 已经是 OnInitListener。您可以在 TextViewClick 中尝试此操作:
您的 TextSpeaker 也会发生变化:
此外,请确保您已在设备设置中启用 TTS。
It appears that your Context is never initialized. Make sure that you initialize the context before you call the constructor for TextSpeaker.
You also don't need to pass an onInitListener, your TextSpeaker is already an OnInitListener. You can try this in your TextViewClick:
And your TextSpeaker would change as well:
In addition, make sure you have enabled TTS in your device settings.