0

我正在创建一个涉及文本消息的 android 应用程序,其中一个功能是在输入时读出每个字母。我正在使用TextToSpeech

我让它在一个测试项目中工作,但是当将它集成到我自己的项目中时,我收到了一个未知的异常

谁能看到可能导致这种情况的原因并提出补救措施?我创建了一个名为speech 的类,它在另一个类中实例化,并且有一个调用speak out 的方法(类似于下面的第二个示例。)还有谁能解释为什么我需要在第一个示例中扩展Activity?同样有问题的行说"new TextToSpeech is not defined"

片段在哪里不起作用。错误开启tts = new TextToSpeech(this, this);

public class Speech extends Activity implements TextToSpeech.OnInitListener {

    private TextToSpeech tts;
    private String toRead;

    public Speech(String toRead){

        this.toRead = toRead;
        tts = new TextToSpeech(this, this);
    }

这是它在哪里工作的代码

public class MainActivity extends Activity implements
        TextToSpeech.OnInitListener {
    /** Called when the activity is first created. */

    private TextToSpeech tts;
    private Button btnSpeak;
    private EditText txtText;

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

        tts = new TextToSpeech(this, this);

        btnSpeak = (Button) findViewById(R.id.btnSendSMS);

        txtText = (EditText) findViewById(R.id.txtMessage);

        // button on click event
        btnSpeak.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                speakOut();
            }

        });
    }

    @Override
    public void onDestroy() {
        // Don't forget to shutdown tts!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }

    public void onInit(int status) {

        if (status == TextToSpeech.SUCCESS) {

            int result = tts.setLanguage(Locale.US);

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else {
                btnSpeak.setEnabled(true);
                speakOut();
            }

        } else {
            Log.e("TTS", "Initilization Failed!");
        }

    }

    private void speakOut() {

        String text = txtText.getText().toString();

        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
}

日志信息

02-13 20:00:54.974: D/AndroidRuntime(11127): Shutting down VM
02-13 20:00:54.974: W/dalvikvm(11127): threadid=1: thread exiting with uncaught exception (group=0x411cd300)
02-13 20:00:54.979: E/AndroidRuntime(11127): FATAL EXCEPTION: main
02-13 20:00:54.979: E/AndroidRuntime(11127): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.BT/org.BT.Text_entry}: java.lang.NullPointerException
02-13 20:00:54.979: E/AndroidRuntime(11127):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
02-13 20:00:54.979: E/AndroidRuntime(11127):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
02-13 20:00:54.979: E/AndroidRuntime(11127):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
02-13 20:00:54.979: E/AndroidRuntime(11127):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
02-13 20:00:54.979: E/AndroidRuntime(11127):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-13 20:00:54.979: E/AndroidRuntime(11127):    at android.os.Looper.loop(Looper.java:137)
02-13 20:00:54.979: E/AndroidRuntime(11127):    at android.app.ActivityThread.main(ActivityThread.java:4898)
02-13 20:00:54.979: E/AndroidRuntime(11127):    at java.lang.reflect.Method.invokeNative(Native Method)
02-13 20:00:54.979: E/AndroidRuntime(11127):    at java.lang.reflect.Method.invoke(Method.java:511)
02-13 20:00:54.979: E/AndroidRuntime(11127):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-13 20:00:54.979: E/AndroidRuntime(11127):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-13 20:00:54.979: E/AndroidRuntime(11127):    at dalvik.system.NativeStart.main(Native Method)
02-13 20:00:54.979: E/AndroidRuntime(11127): Caused by: java.lang.NullPointerException
02-13 20:00:54.979: E/AndroidRuntime(11127):    at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:91)
02-13 20:00:54.979: E/AndroidRuntime(11127):    at android.speech.tts.TtsEngines.getDefaultEngine(TtsEngines.java:75)
02-13 20:00:54.979: E/AndroidRuntime(11127):    at android.speech.tts.TextToSpeech.getDefaultEngine(TextToSpeech.java:1235)
02-13 20:00:54.979: E/AndroidRuntime(11127):    at android.speech.tts.TextToSpeech.initTts(TextToSpeech.java:595)
02-13 20:00:54.979: E/AndroidRuntime(11127):    at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:553)
02-13 20:00:54.979: E/AndroidRuntime(11127):    at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:527)
02-13 20:00:54.979: E/AndroidRuntime(11127):    at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:512)
02-13 20:00:54.979: E/AndroidRuntime(11127):    at org.BT.Speech.<init>(Speech.java:17)
02-13 20:00:54.979: E/AndroidRuntime(11127):    at org.BT.Text_entry.<init>(Text_entry.java:48)
02-13 20:00:54.979: E/AndroidRuntime(11127):    at java.lang.Class.newInstanceImpl(Native Method)
02-13 20:00:54.979: E/AndroidRuntime(11127):    at java.lang.Class.newInstance(Class.java:1319)
02-13 20:00:54.979: E/AndroidRuntime(11127):    at android.app.Instrumentation.newActivity(Instrumentation.java:1057)
02-13 20:00:54.979: E/AndroidRuntime(11127):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)
02-13 20:00:54.979: E/AndroidRuntime(11127):    ... 11 more

- - - - - - - - - - - - - - - - - - - - -编辑 - - - - ----------------------------------------------------------- 进一步的研究有建议可能是因为我在 android 上下文之外尝试 Android 框架?这可能是真的吗 - 因为我使用的是课程而不是活动?如果是这样,我将如何解决这个问题?

4

2 回答 2

1

这在我看来是 TextToSpeech (TTS) 的标准示例,可以在互联网上随处找到,它应该可以毫无问题地正常工作。(但是,这并不意味着此代码示例没有任何设计问题。)

也许您使用的 Android 版本太旧或您的手机上没有正确设置 TTS?您是否检查了手机上的 TTS 设置?(通常,如果 TTS 没有在您的手机上正确设置,此代码应该调用正确的安装页面来执行此操作,但我记得看到一条消息,这部分有时会被炸毁。)

txtText您还应该在使用 初始化后检查它是否为空findViewById()

于 2013-02-13T22:17:59.717 回答
0

我就是这样做的。。

public class TextToSpeechActivity extends Activity implements TextToSpeech.OnInitListener
{
private TextToSpeech mTts;
// This code can be any value you want, its just a checksum.
private static final int MY_DATA_CHECK_CODE = 1234;

/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Fire off an intent to check if a TTS engine is installed
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

}

public void speakClicked(View v)
{
// grab the contents of the text box.
EditText editText = (EditText) findViewById(R.id.inputText);

mTts.speak(editText.getText().toString(),TextToSpeech.QUEUE_FLUSH, null);
}

public void onInit(int i)
{

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

@Override
public void onDestroy()
{
if (mTts != null)
{
mTts.stop();
mTts.shutdown();
}
super.onDestroy();
}
}
于 2013-02-13T20:21:08.963 回答