我刚刚开始学习制作Android应用程序,并且正在我的Android Studio上尝试使用此语音识别应用程序。当我按下RUN时,仿真器Api 29打开,显示空白页,然后使用CLOSE APP关闭!窗口。它在我的Android手机Api 24上执行相同的操作。
我经历了与您过去的成员相关的所有可能原因,但没有找到类似的解决方案。
public class MainActivity extends AppCompatActivity{
private static final int MY_PERMISSIONS_RECORD_AUDIO = 1; // MIC PERMISSION VARIABLE
// private static final int REQUEST_RECORD_PERMISSION = 1;
android.speech.tts.TextToSpeech TTS; //variable del TextToSpeech
SpeechRecognizer speechRecog;
private android.widget.ProgressBar progressBar;
private SpeechRecognizer speech = null;
private android.content.Intent recognizerIntent;
private String LOG_TAG = "VoiceRecognitionActivity";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//android.widget.TextView returnedText = (findViewById(R.id.textView1));
progressBar = (findViewById(R.id.progressBar1));
android.widget.ToggleButton toggleButton = (findViewById(R.id.toggleButton1));
progressBar.setVisibility(android.view.View.INVISIBLE);
final SpeechRecognizer speech = SpeechRecognizer
.createSpeechRecognizer(MainActivity.this);
android.util.Log.i(LOG_TAG, "isRecognitionAvailable: " + SpeechRecognizer.isRecognitionAvailable(this));
speech.setRecognitionListener((android.speech.RecognitionListener) this);
recognizerIntent = new android.content.Intent(android.speech.RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizerIntent.putExtra(android.speech.RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
recognizerIntent.putExtra(android.speech.RecognizerIntent.EXTRA_LANGUAGE_MODEL,
android.speech.RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
recognizerIntent.putExtra(android.speech.RecognizerIntent.EXTRA_MAX_RESULTS, 3);
toggleButton.setOnCheckedChangeListener(new android.widget.CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(android.widget.CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
progressBar.setVisibility(android.view.View.VISIBLE);
progressBar.setIndeterminate(true);
} else {
progressBar.setIndeterminate(false);
progressBar.setVisibility(android.view.View.INVISIBLE);
speech.stopListening();
}
}
});
这里是LOGCAT的副本
2019-07-11 14:58:46.703 32610-32610/com.example.speech_recognition E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.speech_recognition, PID: 32610
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.speech_recognition/com.example.speech_recognition.MainActivity}: java.lang.ClassCastException: com.example.speech_recognition.MainActivity cannot be cast to android.speech.RecognitionListener
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3260)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3396)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7319)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
Caused by: java.lang.ClassCastException: com.example.speech_recognition.MainActivity cannot be cast to android.speech.RecognitionListener
at com.example.speech_recognition.MainActivity.onCreate(MainActivity.java:51)
at android.app.Activity.performCreate(Activity.java:7783)
at android.app.Activity.performCreate(Activity.java:7772)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3235)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3396)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7319)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
2019-07-11 14:58:46.739 32610-32610/com.example.speech_recognition I/Process: Sending signal. PID: 32610 SIG: 9
答案 0 :(得分:0)
此行失败:
speech.setRecognitionListener((android.speech.RecognitionListener) this);
问题在于您的MainActivity
未实现接口RecognitionListener
,因此无法将其强制转换为该类型。