簡體   English   中英

為什么我的 Android 應用程序在啟動時崩潰

[英]Why is my android app crashing on start up

我剛剛開始學習制作 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

它在這一行失敗:

speech.setRecognitionListener((android.speech.RecognitionListener) this);

問題是您的MainActivity沒有實現接口RecognitionListener ,因此它不能轉換為該類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM