【问题标题】:Android TTS - Application has stopped unexpectedlyAndroid TTS - 应用程序意外停止
【发布时间】:2012-08-10 01:35:41
【问题描述】:

我已经从在线教程中实现了一个 android TTS 代码。所有教程都有一个文本框和一个提交按钮来捕获它想要说的文本。我的目标是从文件中获取文本然后说话,不需要用户输入(因为这个模块将作为一个类实现,所有用户输入都将发生在主要活动中)。

我实现的代码是

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MyTextToSpeech extends Activity implements OnInitListener{
    /** Called when the activity is first created. */
    private int MY_DATA_CHECK_CODE = 0;

    private TextToSpeech tts;

    private EditText inputText;
    private Button speakButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //inputText = (EditText) findViewById(R.id.input_text);
       // speakButton = (Button) findViewById(R.id.speak_button);

       // speakButton.setOnClickListener(new OnClickListener() {                     

            //public void onClick(View v) {
             //  String text = inputText.getText().toString();
            //   if (text!=null && text.length()>0) {
                  //  Toast.makeText(MyTextToSpeech.this, "Saying: " + text, Toast.LENGTH_LONG).show();
//tts.speak(text, TextToSpeech.QUEUE_ADD, null);
                 //   
               //}
           // }
        //});
        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
        tts.speak("hi this is a test", TextToSpeech.QUEUE_ADD, null); //Added by me
   }

   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);
            }
     }
   }

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

在上面的代码中,我已经注释掉了从用户那里获取文本的行。当 'tts.speak' 方法在 onClick 方法中时,代码运行良好。当我最后把它弄出来时,代码行为不端并给出“空指针异常”并关闭我的应用程序。

如何解决上述问题。

非常感谢。

PS:如果我之前使用命令 tts = new TextToSpeech(this, this);在主要活动中,空指针异常不存在,但应用程序不说话。

【问题讨论】:

    标签: android nullpointerexception text-to-speech


    【解决方案1】:

    您正在 onActivityResult() 方法中初始化变量 tts

    您在onCreate() 方法中调用TextToSpeech.Engine.ACTION_CHECK_TTS_DATA 活动,当该活动完成时,您的onActivityResult() 方法被调用。

    这适用于您已注释掉的代码,因为在 TTS 检查活动完成并返回到您的活动并且您已设置 tts = new TextToSpeech(this, this); 之前,用户实际上无法单击按钮使其说话。

    在您发布的代码中,您在将变量 tts 设置为任何值之前调用了 tts.speak()——这是获得 NullPointerException 的可靠方法。

    此外,您需要确保在您尝试使用您的 tts 参考说话之前,已经进行了 onInit() 回调。这就是为什么,当您之前初始化 tts 引用时,NullPointerException 消失但它不说话(因为尚未发生 onInit() 回调)。

    【讨论】:

    • 非常感谢您的解释。它现在正在运行。我将 tts.speak 放在 onInit() 方法中。
    • 一个问题)我上面的代码中的哪个语句调用了onInit() 方法?这对我将来会有帮助,这样我就不会犯类似的错误。
    • 当您像这样创建TextToSpeech 对象时:tts = new TextToSpeech(this, this);。这将启动 TextToSpeech 对象的初始化。当初始化完成并且TextToSpeech 对象准备好接受请求时,它会调用您的onInit() 方法。
    猜你喜欢
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多