两个onClickListeners。无法启动程序

时间:2014-10-03 18:41:23

标签: java android

我想编程两个按钮。一个人应该认出我的声音而另一个应该使用文字转语音功能。当我说话时,文字转到text2,tts意思是这个文本。当我单独编写代码(一个项目中的VoiceRecognition,另一个项目中的TT)时,它可以工作。但我想加入这两个函数,但是当我启动程序时它会立即崩溃。

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

1 个答案:

答案 0 :(得分:1)

您的buttonSpeak和buttonHear在您的活动中被声明为按钮,但是从您的logcat开始,看起来您正在使用它们指向ImageButton对象(至少一个或两个)。

由于您打算使用图像按钮,将活动中buttonHear和buttonSpeak的引用更改为ImageButton(但这只是一个建议)。