错误:无法执行 android:onClick 方法。

huangapple go评论102阅读模式
英文:

Error: Could not execute method for android:onClick

问题

以下是您提供的代码的翻译部分:

  1. @RequiresApi(api = Build.VERSION_CODES.KITKAT)
  2. public static void ssmlToAudio(String ssmlText, String outFile) throws Exception {
  3. // 实例化客户端
  4. try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
  5. // 将 SSML 文本设置为合成输入
  6. SynthesisInput input = SynthesisInput.newBuilder().setSsml(ssmlText).build();
  7. // 构建语音请求,选择语言代码("en-US")和 SSML 语音性别("male")
  8. VoiceSelectionParams voice =
  9. VoiceSelectionParams.newBuilder()
  10. .setLanguageCode("en-US")
  11. .setSsmlGender(SsmlVoiceGender.MALE)
  12. .build();
  13. // 选择音频文件类型
  14. AudioConfig audioConfig =
  15. AudioConfig.newBuilder().setAudioEncoding(AudioEncoding.MP3).build();
  16. // 使用所选语音参数和音频文件类型对文本输入执行文本到语音请求
  17. SynthesizeSpeechResponse response =
  18. textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);
  19. // 从响应中获取音频内容
  20. ByteString audioContents = response.getAudioContent();
  21. // 将响应写入输出文件
  22. try (OutputStream out = new FileOutputStream(outFile)) {
  23. out.write(audioContents.toByteArray());
  24. System.out.println("音频内容已写入文件 " + outFile);
  25. }
  26. }
  27. }
  1. @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  2. public void onClick(View view) throws Exception {
  3. ssmlToAudio("你好", "测试");
  4. }

如果我运行应用程序并点击按钮,我会收到以下错误:

java.lang.IllegalStateException: 无法执行 android:onClick 方法

我做错了什么?

英文:

I have found this code on a Google documentation page (Android Studio changed it a bit automatically):

  1. @RequiresApi(api = Build.VERSION_CODES.KITKAT)
  2. public static void ssmlToAudio(String ssmlText, String outFile) throws Exception {
  3. // Instantiates a client
  4. try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
  5. // Set the ssml text input to synthesize
  6. SynthesisInput input = SynthesisInput.newBuilder().setSsml(ssmlText).build();
  7. // Build the voice request, select the language code ("en-US") and
  8. // the ssml voice gender ("male")
  9. VoiceSelectionParams voice =
  10. VoiceSelectionParams.newBuilder()
  11. .setLanguageCode("en-US")
  12. .setSsmlGender(SsmlVoiceGender.MALE)
  13. .build();
  14. // Select the audio file type
  15. AudioConfig audioConfig =
  16. AudioConfig.newBuilder().setAudioEncoding(AudioEncoding.MP3).build();
  17. // Perform the text-to-speech request on the text input with the selected voice parameters and
  18. // audio file type
  19. SynthesizeSpeechResponse response =
  20. textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);
  21. // Get the audio contents from the response
  22. ByteString audioContents = response.getAudioContent();
  23. // Write the response to the output file
  24. try (OutputStream out = new FileOutputStream(outFile)) {
  25. out.write(audioContents.toByteArray());
  26. System.out.println("Audio content written to file " + outFile);
  27. }
  28. }
  29. }

I would like to run this method on a click event. So this is what I have tried so far:

  1. @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  2. public void onClick(View view) throws Exception {
  3. ssmlToAudio("Hello", "test");
  4. }

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.

huangapple
  • 本文由 发表于 2020年10月21日 06:26:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64454179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定