android文本到语音(tts)

时间:2012-09-23 22:00:40

标签: android

我为我编写了这个应用程序,因为我想了解如何使用Android上的tts引擎。

然而,当我启动应用程序时,我会在我的手机和模拟器上:“不幸的是你的应用程序必须停止”,我还附加了Logcat,我希望有人在这里帮助我

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

这是我的清单:

<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>

2 个答案:

答案 0 :(得分:1)

您的NullPointerException方法的第26行有一个onCreate(...)。这似乎是......

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

基本上看起来你的Bundle bnull,除非Activity已经通过传递任何额外数据的东西启动,否则无论如何都是如此。

答案 1 :(得分:0)

我认为logcat的相关行是这些:

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

由于第26行的某些内容发生了崩溃。您可能需要验证来自Bundle的{​​{1}}是否为空,以及您从{获得的额外“名称” {1}}确实存在。