1

我为我编写了这个应用程序,因为我想了解如何在 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();
    }
}

}

日志猫:

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

2 回答 2

1

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

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

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

于 2012-09-23T22:12:18.643 回答
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 行的某些事情。您可能想要验证BundlefromgetExtras()不为 null 并且您从 the 获得的额外“名称”Bundle确实存在。

于 2012-09-23T22:09:31.540 回答