如何解决错误:无法执行android:onClick的方法
我在Google documentation page上找到了此代码(Android Studio自动对其进行了一些更改):
@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);
}
}
}
我想在click事件上运行此方法。所以这是我到目前为止尝试过的:
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void onClick(View view) throws Exception {
ssmlToAudio("Hello","test");
}
但是,如果我运行我的应用程序并单击一个按钮,则会收到此错误:
我在做什么错了?
解决方法
您必须在活动中实现onClickListener
,然后重写onClick
方法。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。