gpt4 book ai didi

java - 两个 onClickListener。无法启动程序

转载 作者:行者123 更新时间:2023-12-01 12:28:00 24 4
gpt4 key购买 nike

我想对两个按钮进行编程。一个应该识别我的声音,另一个应该使用文本转语音功能。当我说话时,文本会转到 text2,tts 的意思是获取此文本。当我单独编写代码时(一个项目中的语音识别,另一个项目中的 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

最佳答案

您的buttonSpeak和buttonHear在您的 Activity 中被声明为按钮,但从您的logcat来看,您似乎正在使用它们来指向ImageButton对象(至少其中一个或可能两个)。

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

关于java - 两个 onClickListener。无法启动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26184830/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com