0
 String toSpeak=null;    

 public void speakText(View view){

    //String toSpeak = write.getText().toString();
    Toast.makeText(getApplicationContext(), toSpeak,
            Toast.LENGTH_SHORT).show();
    ttobj.speak(toSpeak, TextToSpeech.QUEUE_ADD, null);

}

And i am calling like this

 public void onCreate(Bundle savedInstanceState) 
   {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.receive_payment);

    toSpeak="Hello Mr Prawin";
   speakText();

}

why because i want to call this function On load Layout Event

  • Are you asking what to put in for the `param` of `speakText()`? If so, use `this` in that situation. That refers to your `Activity Context`. And you might want to [read this](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved=0CCcQFjAB&url=http%3A%2F%2Fwww.doubleencore.com%2F2013%2F06%2Fcontext%2F&ei=4nH7U9TVCcn8yQTI64CYBg&usg=AFQjCNEQHYljsmkUjnhhgr2Wyq8534_wxw&sig2=fUVbsmsEpeTjy8LAJHBVUw&bvm=bv.73612305,d.aWw) – codeMagic Aug 25 '14 at 17:24
  • On load Page I am Calling TextToSpeach Function with My Self Generated Text Pls Help Me – Praveen Kumar Aug 26 '14 at 06:20

3 Answers3

0

You can get the context by invoking getApplicationContext(), getContext(), getBaseContext() or this (when in the activity class).

So pass this as actvity context in your function as

 speakText(this);

For more info see What is Context in Android?

Edit

you want speakText on button click so add android:onClick="speakText" under button tag in your xml layout as

 <Button
      android:id="@+id/button1"
        android:onClick="speakText" />

And remove speakText(); from onCreate method.

Community
  • 1
  • 1
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
0

What I can see that, speakTest method is your own method, and you are not using the view object in your method. Then why don't you just remove view parameter from your method. If you can not for some reason then simply pass null for view because view is not being used anywhere.

Bhawna Raheja
  • 524
  • 6
  • 9
0
 I Got Solution 
public class Receive_Payment extends Activity implements
    TextToSpeech.OnInitListener {  // I am Calling From Here
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   {
     * **  Somthing
             text="Hello Prawin";
              speakOut()
   }

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);
        //int result=tts.setLanguage(new Locale("hin"));
        if (result == TextToSpeech.LANG_MISSING_DATA || result ==         

      TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("TTS", "This Language is not supported");
        } else {
           // btnSpeak.setEnabled(true);
            speakOut();
        }

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

}

private void speakOut() {

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