I'm attempting to create a TextToSpeech manager class.
I'm getting a Null Pointer Exception error when the class’s constructor is called.
The TTS managing class accepts a single Context parameter.
The MainActivity class attempts to instantiate the object by calling
TTS_Manager tts = new TTS_Manager(this);
Logcat shows the TTS_Manager class’s constructor being called and then the NPE:
TextToSpeech ttsVoice = new TextToSpeech(context, this);
My take is that there is something wrong with the way the context is being handled.
package com.example.TTS01;
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
public class TTS_Manager implements TextToSpeech.OnInitListener{
TextToSpeech ttsVoice;
public void TTS_Manager(Context context){
//Null Pointer Exception thrown on next line
TextToSpeech ttsVoice = new TextToSpeech(context, this);
ttsVoice.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String utteranceID) {
}
@Override
public void onDone(String utteranceID) {
}
@Override
public void onError(String utteranceID) {
}
});
}
@Override
public void onInit(int i) {
}
}