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?


-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to