从不同的类调用TextToSpeech [英] Calling TextToSpeech from a different class

查看:85
本文介绍了从不同的类调用TextToSpeech的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在另一个类中调用TextToSpeech. 这是我的班级现在的样子:

I am trying to call TextToSpeech in a different class. Here are what my classes look like right now:

//MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  private SpeechRecognizer sr;
  sr.setRecognitionListener(new Listener());
}


//Listener.java
public class Listener implements RecognitionListener() {
  public void onResults(Bundle 
    MainActivity theMainActivity = new MainActivity();

    //the following line always breaks the code:
    tts = new TextToSpeech(theMainActivity, new TextToSpeech.OnInitListener() {/*...*/});
  }
}

对于上下文,该文件在其上下文中为

For context, the file in its context is on GitHub. The version on GitHub is the working version where everything is in MainActivity.java, but I am attempting to move Listener out into its own class Listener.java.

收到的错误是java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference.

现在我要对我尝试过的内容进行详细的解释...

Now for my long explanation of what I have tried...

当我尝试用MainActivity.this替换theMainActivity并得到错误MainActivity is not an enclosing class时.我知道我可以将Listener设为静态类(但不会编译),也可以实例化新的MainActivity().new Listener()(但不会编译). 有人在此处询问了完全相同的问题,该问题本身被标记为重复.所以我的问题是重复的重复...但是没有给出明确的答案.

When I tried replacing theMainActivity with MainActivity.this and get the error MainActivity is not an enclosing class. I understood that I could either make Listener a static class (but won't compile), or I could instantiate new MainActivity().new Listener() (but won't compile). Someone asked the exact same question here which itself is marked a duplicate. So my question is a duplicate of a duplicate... However there was no explicit answer given.

我了解我应该以某种方式解决NullPointerException .但是,当我在使用前记录该值时,它根本不是null.而是theMainActivityLog.d中具有值com.package.name.MainActivity@b76325e.因此,如果它不是一开始就不是null而是引发了null错误,那么如何解决?

I understand that I'm supposed to somehow solve the NullPointerException. However, when I log the value right before usage it is not null at all. Instead theMainActivity has value com.package.name.MainActivity@b76325e in Log.d. So if it's not null in the first place, yet throws a null error, how could it be fixed?

因此,我认为它可能是特定于Android的.毕竟,它与android ContentResolver有关... 因此,我阅读了问题,并认为创建一个应用程序 context 可以工作,除了我的MainActivity扩展了AppCompactActivity而不是Application所以我不能写MainActivity.context.

So I figured maybe it's Android-specific. After all, it has something to do with android ContentResolver... So I read this question and thought that creating an application context would work, except that my MainActivity extends AppCompactActivity instead of Application so I cannot write MainActivity.context.

我了解到我对Java的一些基本了解尚不足.我知道应用程序有一个应用程序和活动"上下文.我知道问题出在Listener.java中.我还以某种方式需要将上下文带到Listener.java中.而且我知道在Listener.java中仅运行new MainActivity()可能不是一个好主意,因为我应该使用原始的MainActivity Activity上下文,该上下文首先调用onResults.我能想到的最好的解决方案是以某种方式使上下文成为全局",可以由任何类或类似的类访问……但是我一直遇到诸如它不是静态的"和不,你不知道"的陷阱.无法使用android.content.Context进行调用,因为您未使用android.app.Application".

I understand that I am missing out on some fundamental Java understanding. I get that there is an Application and Activity context for applications. I get that the problem has to do with getting context in Listener.java. I also get that somehow I need to bring the context into Listener.java. And I know that simply running new MainActivity() in Listener.java is probably a bad idea because I should be using the original MainActivity Activity context which called onResults in the first place. The best solution I can think of is somehow making the context a 'global' in some way that can be accessed by any class, or something like that... but I keep running into pitfalls like "it's not static" and "no you can't call use android.content.Context because you're not using android.app.Application".

有什么想法吗?

推荐答案

尝试一下

//Listener.java
public class Listener implements RecognitionListener() {
 MainActivity instance;
 public Listener(MainActivity mainActivity){
        this.instance = mainActivity;
 }
  public void onResults(Bundle 
    MainActivity theMainActivity = new MainActivity();

    //the following line always breaks the code:
    tts = new TextToSpeech(instance, new TextToSpeech.OnInitListener() {/*...*/});
  }
}
//MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  private SpeechRecognizer sr;
  sr.setRecognitionListener(new Listener(this));
}

这篇关于从不同的类调用TextToSpeech的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆