The Android app crashes when adding TTS code

advertisements

I'm new to Android development and I'm trying to run the basic text to speech (TTS) code on (android 4.4 platform and Samsung Galaxy S4) but the app crashes once it is opened. The main activity code and a capture screen of the system message are given.

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

the image is on this link http://i.stack.imgur.com/CYvz2.jpg

this is the red text in the logcat:

04-15 23:38:41.026: E/AndroidRuntime(32189): FATAL EXCEPTION: main 04-15 23:38:41.026: E/AndroidRuntime(32189): Process: com.example.ttscode, PID: 32189 04-15 23:38:41.026: E/AndroidRuntime(32189): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ttscode/com.example.ttscode.MainActivity}: java.lang.NullPointerException 04-15 23:38:41.026: E/AndroidRuntime(32189): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305) 04-15 23:38:41.026: E/AndroidRuntime(32189): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363) 04-15 23:38:41.026: E/AndroidRuntime(32189): at android.app.ActivityThread.access$900(ActivityThread.java:161) 04-15 23:38:41.026: E/AndroidRuntime(32189): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265) 04-15 23:38:41.026: E/AndroidRuntime(32189): at android.os.Handler.dispatchMessage(Handler.java:102) 04-15 23:38:41.026: E/AndroidRuntime(32189): at android.os.Looper.loop(Looper.java:157) 04-15 23:38:41.026: E/AndroidRuntime(32189): at 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): Caused by: 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): at android.app.Activity.performCreate(Activity.java:5426) 04-15 23:38:41.026: E/AndroidRuntime(32189): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 04-15 23:38:41.026: E/AndroidRuntime(32189): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269) 04-15 23:38:41.026: E/AndroidRuntime(32189): ... 11 more 04-15 23:38:47.171: I/Process(32189): Sending signal. PID: 32189 SIG: 9


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

    };

you can use like this:

x.start();

and do not forget give permission for internet:

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