0
我想編程兩個按鈕。一個應該識別我的聲音,另一個應該使用文本轉語音功能。當我說話時,文本轉到text2,tts表示拿這個文本。當我單獨編寫代碼(VoiceRecognition在一個項目中,TT在另一個項目中)時,它可以工作。但我想加入這兩個功能,但是當我啓動該程序時,它立即崩潰。兩個onClickListeners。無法啓動程序
private Button btnSpeak;
private Button btnHear;
private EditText txtText;
private TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(this, this);
// Button for VoiceRecognition
btnHear = (Button)findViewById(R.id.button1);
// OnClickListener for btnHear
btnHear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "de-DE");
try {
startActivityForResult(i, REQUEST_OK);
} catch (Exception e) {
//Toast.makeText(...);
}
}
});
tts = new TextToSpeech(this, this);
// Button for TTS
btnSpeak = (Button) findViewById(R.id.button2);
txtText = (EditText) findViewById(R.id.text2);
// button on click event
btnSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
speakOut();
}
});
}
登錄:
10-03 20:54:58.188: E/AndroidRuntime(9280): FATAL EXCEPTION: main
10-03 20:54:58.188: E/AndroidRuntime(9280): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.logos_daswortgottes/com.example.logos_daswortgottes.MainActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
10-03 20:54:58.188: E/AndroidRuntime(9280): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
10-03 20:54:58.188: E/AndroidRuntime(9280): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
10-03 20:54:58.188: E/AndroidRuntime(9280): at android.app.ActivityThread.access$600(ActivityThread.java:149)
10-03 20:54:58.188: E/AndroidRuntime(9280): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
10-03 20:54:58.188: E/AndroidRuntime(9280): at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 20:54:58.188: E/AndroidRuntime(9280): at android.os.Looper.loop(Looper.java:153)
10-03 20:54:58.188: E/AndroidRuntime(9280): at android.app.ActivityThread.main(ActivityThread.java:4987)
10-03 20:54:58.188: E/AndroidRuntime(9280): at java.lang.reflect.Method.invokeNative(Native Method)
10-03 20:54:58.188: E/AndroidRuntime(9280): at java.lang.reflect.Method.invoke(Method.java:511)
10-03 20:54:58.188: E/AndroidRuntime(9280): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
10-03 20:54:58.188: E/AndroidRuntime(9280): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
10-03 20:54:58.188: E/AndroidRuntime(9280): at dalvik.system.NativeStart.main(Native Method)
10-03 20:54:58.188: E/AndroidRuntime(9280): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
10-03 20:54:58.188: E/AndroidRuntime(9280): at com.example.logos_daswortgottes.MainActivity.onCreate(MainActivity.java:40)
10-03 20:54:58.188: E/AndroidRuntime(9280): at android.app.Activity.performCreate(Activity.java:5020)
10-03 20:54:58.188: E/AndroidRuntime(9280): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-03 20:54:58.188: E/AndroidRuntime(9280): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
10-03 20:54:58.188: E/AndroidRuntime(9280): ... 11 more
你在logcat中遇到什麼錯誤? – 2014-10-03 18:46:36
爲什麼你聲明兩次?!請把logcat – MHP 2014-10-03 18:48:12
在你的xml中你將它設置爲一個ImageButton而不是一個按鈕,但你爲什麼要設置它兩次? – zgc7009 2014-10-03 19:02:56