简单测试中的android文本到语音问题

时间:2014-01-16 17:22:26

标签: android text-to-speech

我正在尝试在我的应用程序中实现TTS。我首先测试了这些内容:http://www.androidhive.info/2012/01/android-text-to-speech-tutorial/ 在我的两台设备上运行良好(4.1.2和4.4) 所以我试图克隆我的Listen活动中的代码...只有改变我做的是摆脱输入TextView并使用现有的字符串。 (后面的文字将来自一个文件......)

我在init方法中得到了一个java.lang.NullPointerException

java.lang.NullPointerException at com.swimtechtest.swimmer.ListenActivity.onInit(ListenActivity.java:74)

这是我的ListenActivity.java,并按照完整的日志跟踪,我在哪里错了? 我发现与tuts代码没什么大不同:

public class ListenActivity extends Activity implements TextToSpeech.OnInitListener {
static final    String TAG = "SWIMMER";

private TextToSpeech    tts;
private Button          btnSpeak;

private final String    mText = "My best wishes for new year 2014";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listen);

    tts = new TextToSpeech(this, this);

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


    // button on click event
    btnSpeak.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            speakOut();
        }

    });
 }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
         finish();
        return true;
    default:
        return super.onOptionsItemSelected(item);   
    }
}

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

@Override
public void onInit(int status) {

    if (status == TextToSpeech.SUCCESS) {

        int result = tts.setLanguage(Locale.getDefault());  
        if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
       } else {
           btnSpeak.setEnabled(true);
         speakOut();
        }

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

}

private void speakOut() {
    tts.setPitch(1.1f);
    tts.setSpeechRate(2);
    tts.speak(mText, TextToSpeech.QUEUE_FLUSH, null);
}

}

这是日志

FATAL EXCEPTION: main
 Process: com.swimtechtest.swimmer, PID: 17603
 java.lang.NullPointerException
at com.swimtechtest.swimmer.ListenActivity.onInit(ListenActivity.java:74)
at android.speech.tts.TextToSpeech.dispatchOnInit(TextToSpeech.java:701)
at android.speech.tts.TextToSpeech.access$1400(TextToSpeech.java:58)
at android.speech.tts.TextToSpeech$Connection$SetupConnectionAsyncTask.onPostExecute(TextToSpeech.java:1509)
at android.speech.tts.TextToSpeech$Connection$SetupConnectionAsyncTask.onPostExecute(TextToSpeech.java:1471)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

我很累..工作时间太晚才能休息......这么愚蠢的错误

我刚刚重新定义了btnSpeakout .... private Button btnSpeak;然后我用了Button btnSpeak =(Button)findViewById(R.id.start_listen);

我应该得到-20 !!但是新年快乐!!!