英文:
Error: Could not execute method for android:onClick
问题
以下是您提供的代码的翻译部分:
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static void ssmlToAudio(String ssmlText, String outFile) throws Exception {
// 实例化客户端
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
// 将 SSML 文本设置为合成输入
SynthesisInput input = SynthesisInput.newBuilder().setSsml(ssmlText).build();
// 构建语音请求,选择语言代码("en-US")和 SSML 语音性别("male")
VoiceSelectionParams voice =
VoiceSelectionParams.newBuilder()
.setLanguageCode("en-US")
.setSsmlGender(SsmlVoiceGender.MALE)
.build();
// 选择音频文件类型
AudioConfig audioConfig =
AudioConfig.newBuilder().setAudioEncoding(AudioEncoding.MP3).build();
// 使用所选语音参数和音频文件类型对文本输入执行文本到语音请求
SynthesizeSpeechResponse response =
textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);
// 从响应中获取音频内容
ByteString audioContents = response.getAudioContent();
// 将响应写入输出文件
try (OutputStream out = new FileOutputStream(outFile)) {
out.write(audioContents.toByteArray());
System.out.println("音频内容已写入文件 " + outFile);
}
}
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void onClick(View view) throws Exception {
ssmlToAudio("你好", "测试");
}
如果我运行应用程序并点击按钮,我会收到以下错误:
java.lang.IllegalStateException: 无法执行 android:onClick 方法
我做错了什么?
英文:
I have found this code on a Google documentation page (Android Studio changed it a bit automatically):
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static void ssmlToAudio(String ssmlText, String outFile) throws Exception {
// Instantiates a client
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
// Set the ssml text input to synthesize
SynthesisInput input = SynthesisInput.newBuilder().setSsml(ssmlText).build();
// Build the voice request, select the language code ("en-US") and
// the ssml voice gender ("male")
VoiceSelectionParams voice =
VoiceSelectionParams.newBuilder()
.setLanguageCode("en-US")
.setSsmlGender(SsmlVoiceGender.MALE)
.build();
// Select the audio file type
AudioConfig audioConfig =
AudioConfig.newBuilder().setAudioEncoding(AudioEncoding.MP3).build();
// Perform the text-to-speech request on the text input with the selected voice parameters and
// audio file type
SynthesizeSpeechResponse response =
textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);
// Get the audio contents from the response
ByteString audioContents = response.getAudioContent();
// Write the response to the output file
try (OutputStream out = new FileOutputStream(outFile)) {
out.write(audioContents.toByteArray());
System.out.println("Audio content written to file " + outFile);
}
}
}
I would like to run this method on a click event. So this is what I have tried so far:
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void onClick(View view) throws Exception {
ssmlToAudio("Hello", "test");
}
But if I run my app and click on a button, I'll get this error:
> java.lang.IllegalStateException: Could not execute method for
> android:onClick
What am I doing wrong?
答案1
得分: 1
你必须在你的活动中实现onClickListener
,然后重写onClick
方法。
英文:
You have to implement the onClickListener
in your activity and then override the onClick
method.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论