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

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

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.

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:

确定