我尝试使用Text-to-Speech,但是得到了一个nullpointerexception。我有很多带有text init的textviews。我试图做的是,当用户单击此textviews时,text-to-speech会说出该特定文本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)

最佳答案

您的上下文似乎从未初始化。在调用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。

08-06 11:26