无法从非活动课程中获得文本转语音工作

发布于 2024-12-27 20:11:44 字数 2816 浏览 0 评论 0原文

我尝试使用文本转语音,但出现空指针异常。我有许多带有文本初始化的文本视图。我尝试做的是,当用户单击此文本视图时,文本转语音会说出该特定内容上的文本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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

歌枕肩 2025-01-03 20:11:44

看来您的上下文从未初始化。确保在调用 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。

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:

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);
  }
}

And your TextSpeaker would change as well:

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);
    }
  }
}

In addition, make sure you have enabled TTS in your device settings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文