2014-10-03 31 views
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 
+0

你在logcat中遇到什麼錯誤? – 2014-10-03 18:46:36

+2

爲什麼你聲明兩次?!請把logcat – MHP 2014-10-03 18:48:12

+0

在你的xml中你將它設置爲一個ImageButton而不是一個按鈕,但你爲什麼要設置它兩次? – zgc7009 2014-10-03 19:02:56

回答

1

你buttonSpeak和buttonHear中被聲明爲您的活動,但是從你的logcat的按鈕,它看起來像您使用它們指向的ImageButton對象(至少無論是一個或可能兩個)。

由於它看起來像打算使用圖像按鈕,因此將活動中的buttonHear和buttonSpeak的引用更改爲ImageButton(但這只是一個建議)。