Error: Could not execute method for android:onClick
Error: Could not execute method for 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);
}
}
}
我想 运行 在单击事件上使用此方法。所以这就是我到目前为止所尝试的:
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void onClick(View view) throws Exception {
ssmlToAudio("Hello", "test");
}
但是如果我 运行 我的应用程序并点击一个按钮,我会得到这个错误:
java.lang.IllegalStateException: Could not execute method for
android:onClick
我做错了什么?
您必须在 activity 中实现 onClickListener
,然后覆盖 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);
}
}
}
我想 运行 在单击事件上使用此方法。所以这就是我到目前为止所尝试的:
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void onClick(View view) throws Exception {
ssmlToAudio("Hello", "test");
}
但是如果我 运行 我的应用程序并点击一个按钮,我会得到这个错误:
java.lang.IllegalStateException: Could not execute method for android:onClick
我做错了什么?
您必须在 activity 中实现 onClickListener
,然后覆盖 onClick
方法。