TextToSpeech显示空指针异常?

时间:2012-04-08 09:47:42

标签: android nullpointerexception text-to-speech

我正在TextToSpeech上创建一个应用程序。但是当我试图运行时,它会在后续行上抛出异常。

tts.speak(ruleOne, TextToSpeech.QUEUE_ADD, null);

Here is my full code for reference
请给我任何提示。我不明白我的代码有什么问题。当我编译我的代码时,所有值都正确传递但当它跳转到tts对象上方时它会命中空指针异常。是什么语法错了?哪个方法首先调用onCreate()或onActivityResult? 在此先感谢

这是Logcat内容。

04-10 13:58:34.082: WARN/System.err(19352): java.lang.NullPointerException
04-10 13:58:34.082: WARN/System.err(19352):     at com.example.examguide.ExamRulesActivity.onCreate(ExamRulesActivity.java:60)
04-10 13:58:34.082: WARN/System.err(19352):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-10 13:58:34.092: WARN/System.err(19352):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-10 13:58:34.092: WARN/System.err(19352):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-10 13:58:34.092: WARN/System.err(19352):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-10 13:58:34.092: WARN/System.err(19352):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-10 13:58:34.092: WARN/System.err(19352):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-10 13:58:34.092: WARN/System.err(19352):     at android.os.Looper.loop(Looper.java:123)
04-10 13:58:34.092: WARN/System.err(19352):     at android.app.ActivityThread.main(ActivityThread.java:3683)
04-10 13:58:34.102: WARN/System.err(19352):     at java.lang.reflect.Method.invokeNative(Native Method)
04-10 13:58:34.102: WARN/System.err(19352):     at java.lang.reflect.Method.invoke(Method.java:507)
04-10 13:58:34.102: WARN/System.err(19352):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-10 13:58:34.102: WARN/System.err(19352):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-10 13:58:34.102: WARN/System.err(19352):     at dalvik.system.NativeStart.main(Native Method)

4 个答案:

答案 0 :(得分:10)

tts = new TextToSpeech(this, this);

在onCreate()方法的if循环中。

答案 1 :(得分:3)

建议 Dhruvisha andy boot 时,NullPointerException很明显,因为onCreateonActivityResult之前执行。我的建议是在检查后将所有使用tts的代码带到一个过程。这是我的建议代码:

    @Override
    public void onCreate(Bundle me)
    {
        super.onCreate(me);
        setContentView(R.layout.examrules);
               Intent checkIntent = new Intent();
          checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
          startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

    }
    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);
                this.speak();
            } 
            else {
                // missing data, install it
                Intent installIntent = new Intent();
                installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);

               //tts.isLanguageAvailable(Locale.INDIA_HINDI);
                startActivity(installIntent);

            }
        }

    }

    @Override
    public void onInit(int status) 
    {        
        if (status == TextToSpeech.SUCCESS) 
        {
            // tts.setLanguage(Locale.US);

            Locale loc = new Locale ("IN", "en");
            tts.setLanguage(loc);
            Toast.makeText(ExamRulesActivity.this, 
                    "Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();
        }
        else if (status == TextToSpeech.ERROR) 
        {
            Toast.makeText(ExamRulesActivity.this, 
                    "Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
        }
    }

    public void speak() {
        int isVoiceEnabled=bundle.getInt("isVoiceEnabled");
        setResult(RESULT_OK, intent);       
        if(isVoiceEnabled==1)
        {
             String ruleOne="hi.";
             String ruleTwo= "How are you"; 
             String ruleThree= "will you meet e=me?"; 
             String ruleFour=  " No,Ok";
                     if (ruleOne!=null && ruleOne.length()>0) 
            {
             tts.speak(ruleOne, TextToSpeech.QUEUE_ADD, null);
             tts.speak(ruleTwo, TextToSpeech.QUEUE_ADD, null);
                 tts.speak(ruleThree, TextToSpeech.QUEUE_ADD, null);
             tts.speak(ruleFour, TextToSpeech.QUEUE_ADD, null);

              }       
        }
    }

}

答案 2 :(得分:2)

onActivityResult 之前是不是 onCreate

因此你的tts对象将为null。在调用tts.speak

之前,添加额外的代码以检查tts是否为null

答案 3 :(得分:0)

您只需复制此行

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

在您撰写setContentView()

后立即写信

这是因为在使用TextToSpeach对象执行任何操作之前您需要检查数据是否已安装。

因此,代码中的错误是您在example调用之前编写的intent中编写了所有新内容。