0

在我的应用程序中,我将图像 1 一个接一个地显示 1 秒。当第一张图像出现时,我想使用 texttospeech 方法来描述该图像。我试过了。但是空指针异常即将到来。这里是代码 -

public class Shapes extends Activity {


    private TextToSpeech mTts;
     int flag=0;
     ImageView iv;
     int myData=1;
     Handler handler = new Handler();
     static int v[]={R.drawable.roundd,R.drawable.rectangle,R.drawable.bluesquare};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shapes);
         iv=(ImageView) findViewById(R.id.imageView1);
             handler.postDelayed(changeImage, 2000);
    }

     Runnable changeImage = new Runnable(){

         @Override
         public void run(){
             if(flag>2)
                handler.removeCallbacks(changeImage);
             else{
                 mTts.speak("hii", TextToSpeech.QUEUE_ADD, null);
                iv.setImageResource(v[flag++]);

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

     };

     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == myData) {
                if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                    // success, create the TTS instance
                    mTts = new TextToSpeech(this, (OnInitListener) this);
                }

                else {
                    // missing data, install it
                    Intent installIntent = new Intent();
                    installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                    startActivity(installIntent);
                }
            }

        }

    public void onInit(int status) {
        // TODO Auto-generated method stub
         if (status == TextToSpeech.SUCCESS) {
             // Toast.makeText(Abcd.this, "Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();
               }
        else if (status == TextToSpeech.ERROR) {
            Toast.makeText(this, "Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
                }

    }

日志信息-

11-29 10:21:07.118: E/AndroidRuntime(461): java.lang.NullPointerException
11-29 10:21:07.118: E/AndroidRuntime(461):  at com.my.KidsEasyLearning.Shapes$1.run(Shapes.java:37)
11-29 10:21:07.118: E/AndroidRuntime(461):  at android.os.Handler.handleCallback(Handler.java:587)
11-29 10:21:07.118: E/AndroidRuntime(461):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-29 10:21:07.118: E/AndroidRuntime(461):  at android.os.Looper.loop(Looper.java:123)
11-29 10:21:07.118: E/AndroidRuntime(461):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-29 10:21:07.118: E/AndroidRuntime(461):  at java.lang.reflect.Method.invokeNative(Native Method)
11-29 10:21:07.118: E/AndroidRuntime(461):  at java.lang.reflect.Method.invoke(Method.java:507)
11-29 10:21:07.118: E/AndroidRuntime(461):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-29 10:21:07.118: E/AndroidRuntime(461):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-29 10:21:07.118: E/AndroidRuntime(461):  at dalvik.system.NativeStart.main(Native Method)
11-29 10:21:09.899: I/Process(461): Sending signal. PID: 461 SIG: 9
4

2 回答 2

1

TextToSpeech 的启动过程可能有点小技巧。我建议使用这个库中的内置代码

https://github.com/gast-lib/gast-lib/blob/master/library/src/root/gast/speech/SpeechRecognizingAndSpeakingActivity.java

于 2012-11-30T15:54:05.773 回答
0

在你的 adtivity 类中实现 TextToSpeech。

并将变量声明为

mTts = new TextToSpeech(this, this);

这些都是明显的错误。请参阅教程以获取指导

于 2012-11-29T04:37:24.797 回答