如何从另一个 activity 调用 TextToSpeech activity?

How to call TextToSpeech activity from another activity?

我有一个 activity (MainActivity) 实现了 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 并传递一个字符串来说话。 我试过了:

MainActivity mainactivity = new MainActivity();
String word;
word = "speak";
mainactivity.speakText2(word); // Error

但是,出现错误:

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)

我尝试使用另一个 activity 的意图:

Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("word", word);
startActivity(intent);

但是,出现错误:

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}

我试图在 activity 中实现 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}

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

您必须使用 Intent 来启动 Activity。参见 https://developer.android.com/training/basics/firstapp/starting-activity.html

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

你只能让引擎说话,在onInit完成后,在onInit()中做如下:

if(状态==TextToSpeech.SUCCESS){ speakText2(单词);

}