xRay
xRay

Reputation: 801

Error: Could not execute method for 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?

Upvotes: 0

Views: 151

Answers (1)

Reaz Murshed
Reaz Murshed

Reputation: 24231

You have to implement the onClickListener in your activity and then override the onClick method.

Upvotes: 1

Related Questions