0

So I am attempting to implement text to speech into my app for the first time ever. I continue to get an error thought when the speech is called. Here is the log that I am getting:

01-23 18:52:29.825: E/AndroidRuntime(24380): FATAL EXCEPTION: main
01-23 18:52:29.825: E/AndroidRuntime(24380): Process: com.rcd.league, PID: 24380
01-23 18:52:29.825: E/AndroidRuntime(24380): java.lang.NullPointerException
01-23 18:52:29.825: E/AndroidRuntime(24380):    at com.rcd.league.speakActivity.speakOut(speakActivity.java:60)
01-23 18:52:29.825: E/AndroidRuntime(24380):    at com.rcd.league.spawnTimers$15.onClick(Timers.java:350)
01-23 18:52:29.825: E/AndroidRuntime(24380):    at android.view.View.performClick(View.java:4442)
01-23 18:52:29.825: E/AndroidRuntime(24380):    at android.view.View$PerformClick.run(View.java:18423)
01-23 18:52:29.825: E/AndroidRuntime(24380):    at android.os.Handler.handleCallback(Handler.java:733)
01-23 18:52:29.825: E/AndroidRuntime(24380):    at android.os.Handler.dispatchMessage(Handler.java:95)
01-23 18:52:29.825: E/AndroidRuntime(24380):    at android.os.Looper.loop(Looper.java:137)
01-23 18:52:29.825: E/AndroidRuntime(24380):    at android.app.ActivityThread.main(ActivityThread.java:5083)
01-23 18:52:29.825: E/AndroidRuntime(24380):    at java.lang.reflect.Method.invokeNative(Native Method)
01-23 18:52:29.825: E/AndroidRuntime(24380):    at java.lang.reflect.Method.invoke(Method.java:515)
01-23 18:52:29.825: E/AndroidRuntime(24380):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-23 18:52:29.825: E/AndroidRuntime(24380):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-23 18:52:29.825: E/AndroidRuntime(24380):    at dalvik.system.NativeStart.main(Native Method)

Here is my code:

package com.rcd.league;

import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;

public class speakActivity extends Activity  implements TextToSpeech.OnInitListener {

    private int MY_DATA_CHECK_CODE = 0;
    private TextToSpeech tts;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent checkTTSIntent = new Intent();
        checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
        tts = new TextToSpeech(this, this);
    }

    @Override
    public void onDestroy() {
        // Don't forget to shutdown tts!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }

    @Override
    public void onInit(int status) {

        if (status == TextToSpeech.SUCCESS) {

            int result = tts.setLanguage(Locale.US);

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else {
                speakOut();
            }

        } else {
            Log.e("TTS", "Initilization Failed!");
        }

    }

    public void speakOut() {

        String text = "test";

        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }



}

I am calling speakOut(); from another activity. Here is the clip of that code:

speakActivity speak = new speakActivity();

...

    bt.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                    if (btTimer.getText().toString().contains(":50")){
                        cd[0].start();
                        speak.speakOut();

                    } else {
                        cd[0].cancel();
                        cd[0].onFinish();
                    }

                }
            });

Any help would be greatly appreciated! Thanks!

EDIT: I have also tried QUEUE_ADD instead of QUEUE_FLUSH and that didnt work either :/

Rob
  • 1,162
  • 2
  • 18
  • 43

1 Answers1

2

You created the speakActivity Activity, but in the code you have posted you never started the Activity. Because the activity was never started, onCreate() was never called and therefore tts is not initialized. That is why you get a NullPointerException when you call

tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);

in speakOut().

See this stackoverflow question for more information about when onCreate() is called: Is onCreate called when an Activity object is created?

Community
  • 1
  • 1
mdewitt
  • 2,526
  • 19
  • 23
  • Thank you, that was very helpful actually, I think it has finally clicked what an Intent does.. however, should I startActivity(intent) in the onCreate of the first activity? – Rob Jan 24 '14 at 00:28
  • From the code I see it looks like you want this code to be called when the user clicks a button on the app right? So you would want to start the activity when the user clicks the button. Look at this tutorial for Starting An Activity in android. I think it is a good, more thorough, example of what you want to do that I can explain. http://developer.android.com/training/basics/firstapp/starting-activity.html – mdewitt Jan 24 '14 at 00:42