文本到语音转换抛出classcastexception

时间:2011-06-16 08:42:28

标签: android exception text-to-speech

     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.setBackgroundDrawable(getResources().getDrawable(R.drawable.score4));
                             dynamic_image.setVisibility(0x000000);      
                             timerfunc(dynamic_image);
                         }         
                         }, 2200);
                    enablelayout4();

                }
   }

上面给出的是我的源代码。但是当它运行时它会抛出classcast异常。我希望将文本“Four”转换为语音,当得分为4.plz任何人帮助我...我知道给出下面的行抛出异常的代码。但我知道很热解决它..

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

1 个答案:

答案 0 :(得分:0)

我得到了答案......我在

中给了clicklistener名字
    tts = new TextToSpeech(this,(OnInitListener) clickball);    

实际上我必须在那里给出OnInitListener名称。我已经改变了这样的代码..

fisrt实现TextToSpeech.OnInitListener 并添加了未实现的方法(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

    }
这解决了我的问题...