Java 如何从另一个活动调用TextToSpeech活动?

Java 如何从另一个活动调用TextToSpeech活动?,java,android,text-to-speech,Java,Android,Text To Speech,我有一个活动(main活动),它实现了TextToSpeech,并且工作得很好。调用按钮的onClick时,它会说出在EditText中键入的任何内容 主要活动: public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener{ private TextToSpeech engine; private EditText text; @Override

我有一个活动(
main活动
),它实现了
TextToSpeech
,并且工作得很好。调用按钮的
onClick
时,它会说出在
EditText
中键入的任何内容

主要活动:

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener{

    private TextToSpeech engine;
    private EditText text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (EditText) findViewById(R.id.text);
        engine = new TextToSpeech(this, this);

        Intent i=getIntent();
        Bundle b=i.getExtras();
        word=b.getString("word");
        speakText2(word);
    }

    // speakText is called by onClick button
    public void speakText(View v) {
        String textContents = text.getText().toString();
        engine.speak(textContents, TextToSpeech.QUEUE_FLUSH, null, null);
    }

    public void speakText2(String textContents) {
        engine.speak(textContents, TextToSpeech.QUEUE_ADD, null, null);
    }

    @Override
    public void onInit(int i) {
        if (i == TextToSpeech.SUCCESS) {
            //Setting speech Language
            engine.setLanguage(Locale.ENGLISH);
            engine.setPitch(1);
        }
    }
}
现在,我想从另一个activity调用
MainActivity
,并传递一个字符串来大声说话。 我试过:

但是,获得错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.speech.tts.TextToSpeech.speak(java.lang.CharSequence, int, android.os.Bundle, java.lang.String)' on a null object reference
at MainActivity.speakText2(TTSEngine.java:53)
I/TextToSpeech: Sucessfully bound to com.google.android.tts
W/TextToSpeech: speak failed: not bound to TTS engine
W/TextToSpeech: speak failed: not bound to TTS engine
W/TextToSpeech: speak failed: not bound to TTS engine
I/TextToSpeech: Connected to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}
I/TextToSpeech: Set up connection to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}
我尝试使用其他活动的意图:

Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("word", word);
startActivity(intent);
但是,获得错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.speech.tts.TextToSpeech.speak(java.lang.CharSequence, int, android.os.Bundle, java.lang.String)' on a null object reference
at MainActivity.speakText2(TTSEngine.java:53)
I/TextToSpeech: Sucessfully bound to com.google.android.tts
W/TextToSpeech: speak failed: not bound to TTS engine
W/TextToSpeech: speak failed: not bound to TTS engine
W/TextToSpeech: speak failed: not bound to TTS engine
I/TextToSpeech: Connected to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}
I/TextToSpeech: Set up connection to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}
我试图在我想使用它的活动中实现
TextToSpeech
。但是,这在我第一次调用speakText2并给出错误信息时不起作用:

W/TextToSpeech: speak failed: not bound to TTS engine
I/TextToSpeech: Connected to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}
I/TextToSpeech: Set up connection to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}

在剩下的时间里,它工作得很好。你知道如何解决这个问题吗?

你必须使用
意图
来启动
活动
。看


手动创建
活动
实例时,不会调用
onCreate
方法(以及任何其他生命周期方法)-这就是为什么NPE访问
引擎
属性-它没有初始化。

您必须使用
意图
来启动
活动
。看


当您手动创建
活动
实例时,不会调用
onCreate
方法(以及任何其他生命周期方法)-这就是为什么您让NPE访问您的
引擎
属性的原因-它没有初始化。

您只能让引擎在完成onInit后说话,所以在onInit()中执行以下操作:

if(status==TextToSpeech.SUCCESS){ speakText2(word)


}

在onInit完成后,您只能让引擎说话,在onInit()中执行以下操作:

if(status==TextToSpeech.SUCCESS){ speakText2(word)


}

我认为您需要对服务进行一些研究。我认为您需要对服务进行一些研究。但是,MainActivity没有XML文件。我只想让应用程序说出自己的想法,并保持不变。有什么方法可以做到这一点吗?我尝试使用intent,但又出现了另一个错误。我已经更新了我的问题。@初学者请看,也许您可以在不使用MainActivity的情况下执行此操作-只使用一个单独的类来使用引擎?但是,MainActivity没有XML文件。我只想让应用程序说出自己的想法,并保持不变。有什么方法可以做到这一点吗?我尝试使用intent,但又出现了另一个错误。我已经更新了我的问题。@初学者看到了,也许你可以在没有main活动的情况下完成这项工作-只需要一个单独的类来处理引擎?我尝试使用intent,但又出现了一个错误。我已经更新了我的问题。此外,MainActivity没有XML文件。我只想让应用程序说出自己的想法,并保持不变。有什么方法可以做到这一点吗?@初学者只需在一个活动中完成所有逻辑,或者将主活动转换为服务。如果你需要一个快速解决方案,我建议用一个活动来解决所有问题,但从长远来看,你应该学习服务。我这样做了,但这不是第一次。我已经更新了问题。如何修复它?@初学者更深入地研究了错误,您需要先执行onInit,然后调用speak()@初学者您的问题的标题有点像miseadingI尝试使用意图,但又出现了一个错误。我已经更新了我的问题。此外,MainActivity没有XML文件。我只想让应用程序说出自己的想法,并保持不变。有什么方法可以做到这一点吗?@初学者只需在一个活动中完成所有逻辑,或者将主活动转换为服务。如果你需要一个快速解决方案,我建议用一个活动来解决所有问题,但从长远来看,你应该学习服务。我这样做了,但这不是第一次。我已经更新了问题。如何修复?@初学者查看了更多错误,您需要先执行onInit,然后调用speak()@初学者您的问题标题有点错误