我尝试使用文字转语音但获得nullpointerexception。我有许多文本视图与文本init.what我试图做的是,当用户点击此文本视图而不是文本到语音说出文本是在那个特定的textview.Following是我的两个类的代码处理文本到语音。我也标记了我得到nullpointerexception的行。请告诉我,我做错了什么?!谢谢。
我的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)
答案 0 :(得分:2)
您的Context似乎从未初始化。在调用TextSpeaker的构造函数之前,请确保初始化上下文。
您也不需要传递onInitListener,TextSpeaker已经是OnInitListener。您可以在TextViewClick中尝试此操作:
public class TextViewClick implements OnClickListener
{
public String textHolder;
public TextViewClick(Context c)
{
TextSpeaker tts1 = new TextSpeaker(c);
}
@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);
}
}
你的TextSpeaker也会改变:
public class TextSpeaker implements OnInitListener
{
TextToSpeech tts;
public TextSpeaker(Context c)
{
tts = new TextToSpeech(c, this);
}
@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR){
tts.setLanguage(Locale.US);
}
}
}
此外,请确保您已在设备设置中启用了TTS。