fsdf fsd
fsdf fsd

Reputation: 1321

android text to speech (tts)

i wrote this app for me because i want to understand how to work with tts engine on android.

however when i launch the app i get on my phone and on the emulator :"unfortanetly your app must be stoped", i also attach the Logcat, i hope someone here will help me

package com.example.textspeechdemi;

import java.util.Locale;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.speech.tts.TextToSpeech;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity implements TextToSpeech.OnInitListener {

Intent mIntent;
String value;
private TextToSpeech tts;
int j;

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

    mIntent = getIntent();
    Bundle b = getIntent().getExtras();
    final String name = b.getString("name");
    j = name.toCharArray()[0];

    Intent checkIntent = new Intent();
    checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(checkIntent, RESULT_OK);
    tts = new TextToSpeech(this, this);
    tts.speak("Welcome", TextToSpeech.QUEUE_ADD, null);





    final Thread t1 = new Thread() {
        public void run() {
            try {

                Thread.sleep(10000);
                finish();
                // this.destroy();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                Toast.makeText(getApplicationContext(), e.getMessage(),
                        Toast.LENGTH_SHORT).show();
            }
        }
    };
    t1.start();
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == RESULT_OK) {
        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(this,
                "Write and Listen engine is initialized & Ready",
                Toast.LENGTH_LONG).show();
    } else if (status == TextToSpeech.ERROR) {
        Toast.makeText(this,
                "Error occurred while initializing Text-To-Speech engine",
                Toast.LENGTH_LONG).show();
    }
}

protected void onDestroy() {
    super.onDestroy();
    if (tts != null) {
        tts.shutdown();
    }
}

}

LOGCAT:

09-23 21:59:16.765: E/AndroidRuntime(731): FATAL EXCEPTION: main
09-23 21:59:16.765: E/AndroidRuntime(731): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.textspeechdemi/com.example.textspeechdemi.MainActivity}: java.lang.NullPointerException
09-23 21:59:16.765: E/AndroidRuntime(731):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-23 21:59:16.765: E/AndroidRuntime(731):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-23 21:59:16.765: E/AndroidRuntime(731):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-23 21:59:16.765: E/AndroidRuntime(731):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-23 21:59:16.765: E/AndroidRuntime(731):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-23 21:59:16.765: E/AndroidRuntime(731):  at android.os.Looper.loop(Looper.java:137)
09-23 21:59:16.765: E/AndroidRuntime(731):  at android.app.ActivityThread.main(ActivityThread.java:4424)
09-23 21:59:16.765: E/AndroidRuntime(731):  at java.lang.reflect.Method.invokeNative(Native Method)
09-23 21:59:16.765: E/AndroidRuntime(731):  at java.lang.reflect.Method.invoke(Method.java:511)
09-23 21:59:16.765: E/AndroidRuntime(731):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-23 21:59:16.765: E/AndroidRuntime(731):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-23 21:59:16.765: E/AndroidRuntime(731):  at dalvik.system.NativeStart.main(Native Method)
09-23 21:59:16.765: E/AndroidRuntime(731): Caused by: java.lang.NullPointerException
09-23 21:59:16.765: E/AndroidRuntime(731):  at com.example.textspeechdemi.MainActivity.onCreate(MainActivity.java:26)
09-23 21:59:16.765: E/AndroidRuntime(731):  at android.app.Activity.performCreate(Activity.java:4465)
09-23 21:59:16.765: E/AndroidRuntime(731):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-23 21:59:16.765: E/AndroidRuntime(731):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-23 21:59:16.765: E/AndroidRuntime(731):  ... 11 more

and this is my manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.textspeechdemi"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
    <uses-permission android:name="android.permission.UPDATE_DEVICE_STATS" />
        <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
    <uses-permission android:name="android.permission.READ_CONTACTS" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
</manifest>

Upvotes: 1

Views: 4783

Answers (2)

Squonk
Squonk

Reputation: 48871

You have a NullPointerException at line 26 of your onCreate(...) method. This seems to be...

final String name = b.getString("name");

Basically it looks like your Bundle b is null which, unless the Activity has been started by something which has passed any extra data, is going to be the case anyway.

Upvotes: 1

acj
acj

Reputation: 4821

The relevant lines of the logcat are these, I think:

Caused by: java.lang.NullPointerException 
09-23 21:59:16.765: E/AndroidRuntime(731):  at com.example.textspeechdemi.MainActivity.onCreate(MainActivity.java:26)

The crash is happening because of something on line 26. You may want to verify that the Bundle from getExtras() is not null and that the "name" extra that you're getting from the Bundle is really there.

Upvotes: 0

Related Questions