title: Text to speech constructor crashing the app android
body: <p>In my app I am using fragments and viewpager. I have a separate text to speech class which extends Activity but doesn't have a layout. I have instantiated this class in FragmentAdapter class, which calls speaks function according to the activity currently on the screen. However the text to speech constructor is causing app to crash resulting in force close. I have figured out the culprit in text to speech constructor is the context, although I have used both getApplicationContext() and className.this but still it crashes the app. Here is the code:</p>
  
  <pre><code>public class textToSpeech extends Activity{
  
  TextToSpeech tts=null;
  
  public textToSpeech()
  {
      Log.d("text to speech", "constructor");
  
      tts = new TextToSpeech(textToSpeech.this , new TextToSpeech.OnInitListener() {
  
          @Override
          public void onInit(int status) {
  
              Log.d("text to speech", "init");
  
              if(status != TextToSpeech.ERROR) 
              {
                  Log.d("text to speech", "condition");
  
                  tts.setPitch(1.1f); 
  
                  tts.setSpeechRate(0.4f); 
  
                  tts.setLanguage(Locale.US);
              }
  
          }
      });
  }
  </code></pre>
  
  <p>Kindly guide me to solve this problem.</p>
  
  <p>I have started text to speech activity using startActivity() , but still I am getting NullPointerException when I call the method in text to speech class. (I have instantiated text to speech class)</p>
  
  <pre><code> tts.SpeakText("hello world"); // in fragmentPagerAdapter exception class
  </code></pre>
  
  <p>In text to speech class</p>
  
  <pre><code>public void SpeakText (String text)
  {
      Log.d("text to speech", "speaking");
  
      if(text!=null)
  
              tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
  }
  </code></pre>
  
  <p>Regards</p>