Java 为什么我的android应用程序在启动时会崩溃
我刚刚开始学习制作android应用程序,我正在我的android studio上尝试这个语音识别应用程序。当我点击RUN时,emulator Api 29打开,显示一个空白页面,然后用CLOSE应用程序关闭!窗户。它在我的android手机Api 24上也做了同样的事情 我检查了所有可能导致您过去成员相关问题的原因,但没有找到类似的解决方案Java 为什么我的android应用程序在启动时会崩溃,java,android,android-activity,methods,Java,Android,Android Activity,Methods,我刚刚开始学习制作android应用程序,我正在我的android studio上尝试这个语音识别应用程序。当我点击RUN时,emulator Api 29打开,显示一个空白页面,然后用CLOSE应用程序关闭!窗户。它在我的android手机Api 24上也做了同样的事情 我检查了所有可能导致您过去成员相关问题的原因,但没有找到类似的解决方案 public class MainActivity extends AppCompatActivity{ private static
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();
}
}
});
这是一份日志
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
它在这方面是失败的:
speech.setRecognitionListener((android.speech.RecognitionListener) this);
问题是您的
main活动
没有实现接口,因此无法将其转换为该类型。日志显示错误在第51行。你能强调一下这一点吗:是由。。。在com.example.speech\u recognition.MainActivity.onCreate(MainActivity.java:51)
这是第51行:speech.setRecognitionListener((android.speech.RecognitionListener)this);那么,谢谢大卫回答我的问题。你刚才说的那句话已经出现在我的节目里了。我在复制粘贴时错过了它。对不起。David,我继续在我的android studio上运行调试器,发现程序在这一行speech.setRecognitionListener((android.speech.RecognitionListener)this)处阻塞;然后,它向我显示了这个警告:未找到用于“Android API 29平台”的反编译.class文件、字节码版本52.0(Java 8)源代码。我验证了android SDK列表,并且确实安装了android 9++(Q)。@AndresVasquez MainActivity不是识别侦听器。如果不实现RecognitionListener或扩展某个实现的类,则无法将其强制转换为RecognitionListener。另一方面,如果您实现了该接口,就不需要强制转换。谢谢。我得到了它!