开发者

text to speech conversion throwing classcast exception

开发者 https://www.devze.com 2023-03-13 14:18 出处:网络
private TextToSpeech tts; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
     private TextToSpeech tts;
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);
        tts = new TextToSpeech(this,(OnInitListener) clickball); 
 }

 OnClickListener clickball=new OnClickListener() {

        @Override
        public void onClick(View v) {

            score=scorenumber.nextInt(8);
            ballid=v.getId();

            if(score==4)
                {
            playgame(ballid,Integer.toString(score));
 dynamic_image.setBackgroundDrawable(getResources().getDrawable(R.drawable.four_01));
                    dynamic_image.setVisibility(0x000000);
                    disablelayout();
                    timerfunc1(dynamic_image,R.drawable.four_02);


                    tts.setLanguage(Locale.US); 
                    tts.speak("Four", TextToSpeech.QUEUE_FLUSH, null); 
                    dynamic_image.postDelayed(new Runnable(){             
                         @Override             
                         public void run() {   
                             dynamic_image.s开发者_StackOverflow社区etBackgroundDrawable(getResources().getDrawable(R.drawable.score4));
                             dynamic_image.setVisibility(0x000000);      
                             timerfunc(dynamic_image);
                         }         
                         }, 2200);
                    enablelayout4();

                }
   }

Given above is my source code.but it is throwing classcast exception when it runs..i want to convert the text "Four" to speech when the score is 4.plz anybody help me...i know given below line of code throwing the exception.but i dnt know hot to solve it..

          tts = new TextToSpeech(this,(OnInitListener) clickball); 


i got the answer...i gave clicklistener name in

    tts = new TextToSpeech(this,(OnInitListener) clickball);    

actuallly i had to give the OnInitListener name there.i had changed the code like this..

fisrt implement TextToSpeech.OnInitListener and added its unimplemented method(OnInit).

 private TextToSpeech tts;
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);
         tts = new TextToSpeech(this,this); 
OnClickListener clickball=new OnClickListener() {

        @Override
        public void onClick(View v) {

            score=scorenumber.nextInt(8);
            ballid=v.getId();
            if (totalovers==0)
            {

                gameover();
                return;
            }
            if(score==4)
                {
                    playgame(ballid,Integer.toString(score));
                    dynamic_image.setBackgroundDrawable(getResources().getDrawable(R.drawable.four_01));
                    dynamic_image.setVisibility(0x000000);
                    disablelayout();
                    timerfunc1(dynamic_image,R.drawable.four_02);

                    currentScore ="FOUR";
                    tts.setLanguage(Locale.US); 
                    tts.speak(currentScore, TextToSpeech.QUEUE_FLUSH, null);

                    dynamic_image.postDelayed(new Runnable(){             
                         @Override             
                         public void run() {   
                             dynamic_image.setBackgroundDrawable(getResources().getDrawable(R.drawable.score4));
                             dynamic_image.setVisibility(0x000000);      
                             timerfunc(dynamic_image);
                         }         
                         }, 2000);
                    enablelayout4();

                }
             }

   @Override      
    public void onInit(int status) {
        // TODO Auto-generated method stub

    }

this solved my problem...

0

精彩评论

暂无评论...
验证码 换一张
取 消