我正在研究应用程序,当新活动开始时,它应该说一些单词但是说话方法总是显示我的空指针异常,但在谷歌搜索,它告诉我,我们可以传递空值。我的方法如下:
private void speakout(String textToSpeak) {
HashMap<String, String> myHashAlarm = new HashMap();
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM,
String.valueOf(AudioManager.STREAM_ALARM));
tts.speak(textToSpeak, TextToSpeech.QUEUE_FLUSH, null);
}
这是我的logcat输出:
11-21 12:14:18.985: I/System.out(307): ip : 10.184.211.143 11-21
12:14:19.035: D/AndroidRuntime(307): Shutting down VM 11-21
12:14:19.035: W/dalvikvm(307): threadid=1: thread exiting with
uncaught exception (group=0x4001d800) 11-21 12:14:19.045:
E/AndroidRuntime(307): FATAL EXCEPTION: main 11-21 12:14:19.045:
E/AndroidRuntime(307): java.lang.RuntimeException: Unable to
instantiate activity
ComponentInfo{com.example.audiobus/com.example.audiobus.AudioBusActivity}:
java.lang.NullPointerException 11-21 12:14:19.045:
E/AndroidRuntime(307): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
11-21 12:14:19.045: E/AndroidRuntime(307): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-21 12:14:19.045: E/AndroidRuntime(307): at
android.app.ActivityThread.access$2300(ActivityThread.java:125) 11-21
12:14:19.045: E/AndroidRuntime(307): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-21 12:14:19.045: E/AndroidRuntime(307): at
android.os.Handler.dispatchMessage(Handler.java:99) 11-21
12:14:19.045: E/AndroidRuntime(307): at
android.os.Looper.loop(Looper.java:123) 11-21 12:14:19.045:
E/AndroidRuntime(307): at
android.app.ActivityThread.main(ActivityThread.java:4627) 11-21
12:14:19.045: E/AndroidRuntime(307): at
java.lang.reflect.Method.invokeNative(Native Method) 11-21
12:14:19.045: E/AndroidRuntime(307): at
...
我的第一个编辑,我打电话说出方法和意图的事情......
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio_bus);
textView = (EditText) findViewById(R.id.ipEditText);
welcomeNote= (TextView) findViewById(R.id.welcomeTextView);
text = welcomeNote.getText().toString();
text.concat(" , Please wait we are retriving information");
System.out.println(text);
speakout(text);
Bundle bunble=getIntent().getExtras();
if(bunble!=null){
//Getting the value stored in the name "IP"
String IP=bunble.getString("IP");
System.out.println(IP);
//appending the value to the contents of textView.
textView.append(" HI "+IP);
}
}
答案 0 :(得分:0)
//在主要活动中
Bundle bundle = new Bundle();
bundle.putString("text-to-speak","Hello world!")
Intent intent = new Intent(<source_activity_context>,<destination_activity_class>);
intent.putExtras(bundle);
startActivity(intent);
//在目标活动中
// onCreateMethod
Bundle bundle = getIntent().getExtras();
String textToSpeak = bundle.getString("text-to-speak");
Intent intent = new Intent(Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(intent, TTS_VAL_DATA);
//全局声明
TextToSpeech tts;
//在onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Engine.CHECK_VOICE_DATA_PASS) {
Toast.makeText(this, "we can use TTS Engine", Toast.LENGTH_LONG)
.show();
tts = new TextToSpeech(this, new OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
tts.setLanguage(Locale.US);
tts.setSpeechRate(1.1f);
tts.speak(textToSpeak,
TextToSpeech.QUEUE_ADD, null);
}
}
});
} else {
Toast.makeText(this, "we can not use TTS Engine", Toast.LENGTH_LONG)
.show();
Intent intent = new Intent(Engine.ACTION_INSTALL_TTS_DATA);
startActivity(intent);
}
super.onActivityResult(requestCode, resultCode, data);
}
希望这会对你有所帮助。最好的问候......