添加TTS代码时Android应用程序崩溃

时间:2014-04-09 22:37:37

标签: android crash text-to-speech

我是Android开发的新手,我尝试在(android 4.4平台和三星Galaxy S4)上运行基本的文本到语音(TTS)代码,但应用程序在打开后崩溃了。给出了主要活动代码和系统消息的捕获屏幕。

package com.example.ttscode;

import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

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.action_settings);

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

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

        @Override
        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();
}

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

图片位于此链接http://i.stack.imgur.com/CYvz2.jpg

这是logcat中的红色文字:

  

04-15 23:38:41.026:E / AndroidRuntime(32189):致命异常:主要   04-15 23:38:41.026:E / AndroidRuntime(32189):进程:com.example.ttscode,PID:32189   04-15 23:38:41.026:E / AndroidRuntime(32189):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.ttscode / com.example.ttscode.MainActivity}:java.lang.NullPointerException   04-15 23:38:41.026:E / AndroidRuntime(32189):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)   04-15 23:38:41.026:E / AndroidRuntime(32189):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)   04-15 23:38:41.026:E / AndroidRuntime(32189):在android.app.ActivityThread.access $ 900(ActivityThread.java:161)   04-15 23:38:41.026:E / AndroidRuntime(32189):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1265)   04-15 23:38:41.026:E / AndroidRuntime(32189):在android.os.Handler.dispatchMessage(Handler.java:102)   04-15 23:38:41.026:E / AndroidRuntime(32189):在android.os.Looper.loop(Looper.java:157)   04-15 23:38:41.026:E / AndroidRuntime(32189):在android.app.ActivityThread.main(ActivityThread.java:5356)   04-15 23:38:41.026:E / AndroidRuntime(32189):at java.lang.reflect.Method.invokeNative(Native Method)   04-15 23:38:41.026:E / AndroidRuntime(32189):at java.lang.reflect.Method.invoke(Method.java:515)   04-15 23:38:41.026:E / AndroidRuntime(32189):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1265)   04-15 23:38:41.026:E / AndroidRuntime(32189):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)   04-15 23:38:41.026:E / AndroidRuntime(32189):at dalvik.system.NativeStart.main(Native Method)   04-15 23:38:41.026:E / AndroidRuntime(32189):引起:java.lang.NullPointerException   04-15 23:38:41.026:E / AndroidRuntime(32189):at com.example.ttscode.MainActivity.onCreate(MainActivity.java:33)   04-15 23:38:41.026:E / AndroidRuntime(32189):在android.app.Activity.performCreate(Activity.java:5426)   04-15 23:38:41.026:E / AndroidRuntime(32189):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)   04-15 23:38:41.026:E / AndroidRuntime(32189):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)   04-15 23:38:41.026:E / AndroidRuntime(32189):... 11更多   04-15 23:38:47.171:I / Process(32189):发送信号。 PID:32189 SIG:9

2 个答案:

答案 0 :(得分:0)

Thread x;
MediaPlayer  mediaPlayer;
 x=new Thread(){    
public void run(){
         try{
             url1="http://www.translate.google.com/translate_tts?ie=UTF-8&q="this is word which is speech"%0A&tl="this is language"&prev=input";


            mediaPlayer=new MediaPlayer();
            mediaPlayer.reset();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setDataSource(url1);
            mediaPlayer.prepare(); // might take long! (for buffering, etc)
            mediaPlayer.start();
                    } catch (IllegalArgumentException e) {
                        mediaPlayer.reset();
                    } catch (IllegalStateException e) {
                        mediaPlayer.reset();
                    } catch (IOException e) {
                        mediaPlayer.reset();
                    } 


            finally{ 
            //  x.suspend();
            } 
     }

    };

你可以像这样使用:

x.start();

并且不要忘记给予互联网许可:

<uses-permission android:name="android.permission.INTERNET" />

答案 1 :(得分:0)

您使用相同的ID在此处查找两个不同的视图:

btnSpeak = (Button) findViewById(R.id.action_settings);
txtText = (EditText) findViewById(R.id.action_settings);

当您尝试将NullPointerException分配给onClickListener

时,看起来会导致btnSpeak