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