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 6766899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:53:12+00:00 2026-05-26T14:53:12+00:00

Well… I am trying to create an application using the TTS Engine. I can

  • 0

Well… I am trying to create an application using the TTS Engine.

I can already make it, work, no problem. However I need my buttons to be dinamic, they will come from a database.

So far you guys have helped me a lot, since now I can do it thanks to the tips I got from you guys.

Well.. now I am stuck again.

Every new button that I create I attach an OnClickListener so it can start the TTS and speak something.

However it’s an inner method, so, when I try to run the code below, it gives me a NullPointerException when it tries to “speak” using the TTS. I know the TTS object is out of context, so, How can I solve this?

Below the code. It’s a little big since I wanted to include everything:

PLEASE JUMP TO THE “HERE IS MY PROBLEM!!!” comment so you guys can see exactly where my problem is. I know where it is, but I don’t know how to solve it =(

Any help is appreciatted! =)

public class LivoxTesteActivity extends Activity implements OnInitListener{
    /** Called when the activity is first created. */
    private int MY_DATA_CHECK_CODE = 0;
    public TextToSpeech tts;


    @Override
    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
            WindowManager.LayoutParams.FLAG_FULLSCREEN);  
    setContentView(R.layout.main);


    LinearLayout lgeral = new LinearLayout (this);
    lgeral.setOrientation(LinearLayout.VERTICAL);
    lgeral.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f));

    String array[][] = {{"Comer","eat", "Eu quero comer", "1"},
            {"Abraço","hug", "Eu quero um abraço", "2"},
            {"Assustado","afraid", "Eu estou com medo", "3"},
            {"Beber","drink", "Eu quero beber", "4"}};
    int x = array.length;

    int qtdeLinhas = 2;
    for (int j = 0; j < qtdeLinhas; j++) {        

        LinearLayout l1 = new LinearLayout (this);
        l1.setOrientation(LinearLayout.HORIZONTAL);
        l1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f));

        FrameLayout fl;
        for (int i = 0; i < array.length; i++) {

            fl = (FrameLayout)LayoutInflater.from(getBaseContext()).inflate(R.layout.framelayoutstyle, l1, false);

            TextView textoEscrito;
            textoEscrito = (TextView)LayoutInflater.from(getBaseContext()).inflate(R.layout.textviewstyle, fl, false);

            textoEscrito.setText(array[i][0]);

            final String texto = textoEscrito.getText().toString();        
            final String textoFalar = array[i][2];

            ImageButton btn;
            btn = (ImageButton)LayoutInflater.from(getBaseContext()).inflate(R.layout.imagebuttonstyle, fl, false);

            btn.setImageResource(this.getResources().getIdentifier("drawable/" + array[i][1], null, this.getPackageName()));


            btn.setOnClickListener(new Button.OnClickListener(){
                public void onClick (View v){


                    Toast.makeText(getBaseContext(), texto, Toast.LENGTH_SHORT).show();
                    //*******************************
                    //HERE IS MY PROBLEM!!!
                    //*******************************
                    tts.speak(txtFl, TextToSpeech.QUEUE_ADD, null);
                    //*******************************
                    //WHEN I TRY TO RUN THE ABOVE IT GIVES A NULLPOINTEREXCEPTION!!!
                    //*******************************



                }

            });


            fl.addView(btn);
            fl.addView(textoEscrito);

            l1.addView(fl);
        }

        lgeral.addView(l1);
    }

    setContentView(lgeral);        

}


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == MY_DATA_CHECK_CODE) {
        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
            // success, create the TTS instance
            tts = new TextToSpeech(this, this);
        }
        else {
            // missing data, install it
            Intent installIntent = new Intent();
            installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installIntent);
        }
    }

}
@Override
public void onInit(int status) {       
    if (status == TextToSpeech.SUCCESS) {
    }
    else if (status == TextToSpeech.ERROR) {
    }
}


@Override
public void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
} 

}

By the way… the method Toast.makeText(getBaseContext(), texto, Toast.LENGTH_SHORT).show(); works fine. I believe that’s because the Toast is a static class.

So, maybe the solution is to create a static class with the method to speak? Ideas? How can I do it?

  • 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-26T14:53:12+00:00Added an answer on May 26, 2026 at 2:53 pm

    Make sure that both onActivityResult and onInit are invoked before you synthesize speech. If NullPointerException is what you’re getting, the most likely cause is that by the time the button is clicked, the tts object has not been constructed yet.

    The right approach is to set up a queue of phrases to speak (like, an ArrayList<String>), and once the TTS is initialized (i. e. in the SUCCESS branch of onInit), start speaking.

    EDIT: now that I look at your code… Where are you invoking any activities that would cause an onActivityResult call? The dev guide suggests doing the following as a part of init process:

    Intent checkIntent = new Intent();
    checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
    

    There’s nothing in the pasted snippet to that effect.

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

Sidebar

Related Questions

well I'm trying to create a great playlist of music, only using a sigle
Well behaved windows programs need to allow users to save their work when they
Well, i have the following need: create 3 dropdownlist with dependencies. My table in
Well, I'm trying to make a graphical game for the first time, but: BufferedImage
Well, this is an interesting problem. I have an ASP.NET MVC3 Intranet application running
Well, it seems simple enough, but I can't find a way to add a
Well this is incredibly frustrating. After being nagged by Rails that I need to
Well, the subject says everything. I'm using json_encode to convert some UTF8 data to
Well, not my server. My friend found it and sent it to me, trying
well I think I know what the problem is. I am just having a

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.