Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4007674
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:39:37+00:00 2026-05-20T08:39:37+00:00

I have this class used in the API demos: public class TextToSpeechActivity extends Activity

  • 0

I have this class used in the API demos:

public class TextToSpeechActivity extends Activity implements TextToSpeech.OnInitListener {

     static int currentHelloIndex = 0;
private static final String TAG = "TextToSpeechDemo";

public static TextToSpeech mTts;
private Button mAgainButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.text_to_speech);

    // Initialize text-to-speech. This is an asynchronous operation.
    // The OnInitListener (second argument) is called after initialization completes.
    mTts = new TextToSpeech(this,
        this  // TextToSpeech.OnInitListener
        );

    // The button is disabled in the layout.
    // It will be enabled upon initialization of the TTS engine.
    mAgainButton = (Button) findViewById(R.id.again_button);

    mAgainButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sayHello();
        }
    });
}

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

    super.onDestroy();
}

// Implements TextToSpeech.OnInitListener.
@Override
public void onInit(int status) {
    // status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
    if (status == TextToSpeech.SUCCESS) {
        // Set preferred language to US english.
        // Note that a language may not be available, and the result will indicate this.
        int result = mTts.setLanguage(Locale.US);
        // Try this someday for some interesting results.
        // int result mTts.setLanguage(Locale.FRANCE);
        if (result == TextToSpeech.LANG_MISSING_DATA ||
            result == TextToSpeech.LANG_NOT_SUPPORTED) {
           // Lanuage data is missing or the language is not supported.
            Log.e(TAG, "Language is not available.");
        } else {
            // Check the documentation for other possible result codes.
            // For example, the language may be available for the locale,
            // but not for the specified country and variant.

            // The TTS engine has been successfully initialized.
            // Allow the user to press the button for the app to speak again.
            mAgainButton.setEnabled(true);
            // Greet the user.
            sayHello();
        }
    } else {
        // Initialization failed.
        Log.e(TAG, "Could not initialize TextToSpeech.");
    }
}


private static final String[] HELLOS = {
  "What is a string of words that satisfy the grammatical rules of a sentence",
  "Salutations",
  "Greetings",
  "Howdy",
  "What's crack-a-lackin?",
  "That explains the stench!"
};


public static void sayHello() {
    // Select a random hello.

    int helloLength = HELLOS.length;
    String hello = HELLOS[currentHelloIndex];
    currentHelloIndex = (currentHelloIndex + 1) % helloLength;
    mTts.speak(hello,
        TextToSpeech.QUEUE_FLUSH,  // Drop all pending entries in the playback queue.
        null);


  }

}

I then have another class with an Onclick button and call the sayHello(); method by using TextToSpeech.sayHello(); This gives me a process closed error and says null exception error in logcat source not found etc. Please can someone help me with this problem. Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:39:38+00:00Added an answer on May 20, 2026 at 8:39 am

    Your current design has a significant flaw. mTts is only initialised in the onCreate method of TextToSpeechActivity, but then you try and call a method on this object in a static method from another class. There is no way to be sure that mTts is not null when your other class calls sayHello(). Further, even if mTts was not null, it had this passed into the constructor, which is your instance of TextToSpeechActivity, so I have no idea if TextToSpeech would even work when created for a totally different Activity.

    I think you need to rethink your class design.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say have this immutable record type: public class Record { public Record(int x,
I have this code inside a class that is used by an application and
I have a Status class in C#, used like this: Status MyFunction() { if(...)
I have this class called Table: class Table { public string Name { get
I have this class. public class Foo { public Guid Id { get; set;
This is probably not possible, but I have this class: public class Metadata<DataType> where
I have this method in my db class public function query($queryString) { if (!$this->_connected)
I know python functions are virtual by default. Let's say I have this: class
I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end
Say I have this simple form: class ContactForm(forms.Form): first_name = forms.CharField(required=True) last_name = forms.CharField(required=True)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.