为什么应用程序会跳过这行代码中的几个问题



我这里有这段代码,旨在让用户回答一系列问题。为了确保语音输出和输入不会冲突,我在每个问题被问到后都实现了延迟。然而,现在该应用程序将直接从第一个问题"你在哪个城市?"跳到最后一个问题"上次你在哪个球队打球?"。当我试图调试这段代码时,我发现该应用程序实际上确实转到了提示中间问题的语音输入的功能。然而,该应用程序只是跳过这些功能,直接跳到最后一个。有人能帮我找到代码中的错误吗?整个活动还有很多代码,所以如果有什么有用的,请告诉我,这样我就可以发布了。

感谢您的帮助:)

private void promptSpeechInput_town() {
        speakWords("Which town are you in?");
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // Do something after 5s = 5000ms
                Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
                STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                        getString(R.string.speech_prompt));
                try {
                    startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_TOWN);
                } catch (ActivityNotFoundException a) {
                    Toast.makeText(getApplicationContext(),
                            getString(R.string.speech_not_supported),
                            Toast.LENGTH_SHORT).show();
                }
            }
        }, 2000);
    }
    private void promptSpeechInput_win() {
        speakWords("Did you win your last game?");
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // Do something after 5s = 5000ms
                Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
                STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                        getString(R.string.speech_prompt));
                try {
                    startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_WIN);
                } catch (ActivityNotFoundException a) {
                    Toast.makeText(getApplicationContext(),
                            getString(R.string.speech_not_supported),
                            Toast.LENGTH_SHORT).show();
                }
            }
        }, 2000);
    }
    private void promptSpeechInput_month() {
        speakWords("What month is it?");
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // Do something after 5s = 5000ms
                Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
                STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                        getString(R.string.speech_prompt));
                try {
                    startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_MONTH);
                } catch (ActivityNotFoundException a) {
                    Toast.makeText(getApplicationContext(),
                            getString(R.string.speech_not_supported),
                            Toast.LENGTH_SHORT).show();
                }
            }
        }, 2000);
    }
    private void promptSpeechInput_day() {
        speakWords("What day is it?");
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // Do something after 5s = 5000ms
                Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
                STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                        getString(R.string.speech_prompt));
                try {
                    startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_DAY);
                } catch (ActivityNotFoundException a) {
                    Toast.makeText(getApplicationContext(),
                            getString(R.string.speech_not_supported),
                            Toast.LENGTH_SHORT).show();
                }
            }
        }, 2000);
    }
    private void promptSpeechInput_team() {
        speakWords("What team did you last play?");
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // Do something after 5s = 5000ms
                Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
                STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                        getString(R.string.speech_prompt));
                try {
                    startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_TEAM);
                } catch (ActivityNotFoundException a) {
                    Toast.makeText(getApplicationContext(),
                            getString(R.string.speech_not_supported),
                            Toast.LENGTH_SHORT).show();
                }
            }
        }, 2000);
    }
    private void promptSpeechInput_confirm_Q1(){
        speakWords("Did you say" + ed23.getText().toString());
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // Do something after 5s = 5000ms
                Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
                STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                        getString(R.string.speech_prompt));
                try {
                    startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_CONFIRM_Q1);
                } catch (ActivityNotFoundException a) {
                    Toast.makeText(getApplicationContext(),
                            getString(R.string.speech_not_supported),
                            Toast.LENGTH_SHORT).show();
                }
            }
        }, 2000);
    }
    private void promptSpeechInput_confirm_Q2(){
        speakWords("Did you say" + ed24.getText().toString());
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // Do something after 5s = 5000ms
                Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
                STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                        getString(R.string.speech_prompt));
                try {
                    startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_CONFIRM_Q2);
                } catch (ActivityNotFoundException a) {
                    Toast.makeText(getApplicationContext(),
                            getString(R.string.speech_not_supported),
                            Toast.LENGTH_SHORT).show();
                }
            }
        }, 2000);
    }
    private void promptSpeechInput_confirm_Q3(){
        speakWords("Did you say" + ed25.getText().toString());
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // Do something after 5s = 5000ms
                Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
                STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                        getString(R.string.speech_prompt));
                try {
                    startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_CONFIRM_Q3);
                } catch (ActivityNotFoundException a) {
                    Toast.makeText(getApplicationContext(),
                            getString(R.string.speech_not_supported),
                            Toast.LENGTH_SHORT).show();
                }
            }
        }, 2000);
    }
    private void promptSpeechInput_confirm_Q4(){
        speakWords("Did you say" + ed26.getText().toString());
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // Do something after 5s = 5000ms
                Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
                STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                        getString(R.string.speech_prompt));
                try {
                    startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_CONFIRM_Q4);
                } catch (ActivityNotFoundException a) {
                    Toast.makeText(getApplicationContext(),
                            getString(R.string.speech_not_supported),
                            Toast.LENGTH_SHORT).show();
                }
            }
        }, 2000);
    }
    private void promptSpeechInput_confirm_Q5(){
        speakWords("Did you say" + ed27.getText().toString());
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // Do something after 5s = 5000ms
                Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
                STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                        getString(R.string.speech_prompt));
                try {
                    startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_CONFIRM_Q5);
                } catch (ActivityNotFoundException a) {
                    Toast.makeText(getApplicationContext(),
                            getString(R.string.speech_not_supported),
                            Toast.LENGTH_SHORT).show();
                }
            }
        }, 2000);
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQ_CODE_SPEECH_INPUT_TOWN:{
                if (resultCode == RESULT_OK && null != data) {
                    ArrayList<String> result_twn = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    ed23.setText(result_twn.get(0));
                    promptSpeechInput_confirm_Q1();
                    break;
                }
            }
            case REQ_CODE_SPEECH_INPUT_WIN:{
                if (resultCode == RESULT_OK && null != data) {
                    ArrayList<String> result_win = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    ed24.setText(result_win.get(0));
                    promptSpeechInput_confirm_Q2();
                    break;
                }
            }
            case REQ_CODE_SPEECH_INPUT_MONTH:{
                if (resultCode == RESULT_OK && null != data) {
                    ArrayList<String> result_month = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    ed25.setText(result_month.get(0));
                    promptSpeechInput_confirm_Q3();
                    break;
                }
            }
            case REQ_CODE_SPEECH_INPUT_DAY:{
                if (resultCode == RESULT_OK && null != data) {
                    ArrayList<String> result_day = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    ed26.setText(result_day.get(0));
                    promptSpeechInput_confirm_Q4();
                    break;
                }
            }
            case REQ_CODE_SPEECH_INPUT_TEAM:{
                if (resultCode == RESULT_OK && null != data) {
                    ArrayList<String> result_team = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    ed27.setText(result_team.get(0));
                    promptSpeechInput_confirm_Q5();
                    break;
                }
            }
            case REQ_CODE_SPEECH_INPUT_CONFIRM_Q1:{
                if (resultCode == RESULT_OK && null != data) {
                    ArrayList<String> result_confirm_Q1 = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    if(result_confirm_Q1.get(0).equals("yes")){
                        promptSpeechInput_win();
                    }
                    else if (!(result_confirm_Q1.get(0).equals("yes")) && !(result_confirm_Q1.get(0).equals("no")) ){
                        promptSpeechInput_confirm_Q1();
                    }
                    else if (result_confirm_Q1.get(0).equals("no")){
                        promptSpeechInput_town();
                    }
                }
            }
            case REQ_CODE_SPEECH_INPUT_CONFIRM_Q2:{
                if (resultCode == RESULT_OK && null != data) {
                    ArrayList<String> result_confirm_Q2 = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    if(result_confirm_Q2.get(0).equals("yes")){
                        promptSpeechInput_month();
                    }
                    else if (!(result_confirm_Q2.get(0).equals("yes")) && !(result_confirm_Q2.get(0).equals("no")) ){
                        promptSpeechInput_confirm_Q2();
                    }
                    else if (result_confirm_Q2.get(0).equals("no")){
                        promptSpeechInput_win();
                    }
                }
            }
            case REQ_CODE_SPEECH_INPUT_CONFIRM_Q3:{
                if (resultCode == RESULT_OK && null != data) {
                    ArrayList<String> result_confirm_Q3 = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    if(result_confirm_Q3.get(0).equals("yes")){
                        promptSpeechInput_day();
                    }
                    else if (!(result_confirm_Q3.get(0).equals("yes")) && !(result_confirm_Q3.get(0).equals("no")) ){
                        promptSpeechInput_confirm_Q3();
                    }
                    else if (result_confirm_Q3.get(0).equals("no")){
                        promptSpeechInput_month();
                    }
                }
            }
            case REQ_CODE_SPEECH_INPUT_CONFIRM_Q4:{
                if (resultCode == RESULT_OK && null != data) {
                    ArrayList<String> result_confirm_Q4 = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    if(result_confirm_Q4.get(0).equals("yes")){
                        promptSpeechInput_team();
                    }
                    else if (!(result_confirm_Q4.get(0).equals("yes")) && !(result_confirm_Q4.get(0).equals("no")) ){
                        promptSpeechInput_confirm_Q4();
                    }
                    else if (result_confirm_Q4.get(0).equals("no")){
                        promptSpeechInput_day();
                    }
                }
            }
            case REQ_CODE_SPEECH_INPUT_CONFIRM_Q5:{
                if (resultCode == RESULT_OK && null != data) {
                    ArrayList<String> result_confirm_Q5 = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    if(result_confirm_Q5.get(0).equals("yes")){
                        movepage();
                    }
                    else if (!(result_confirm_Q5.get(0).equals("yes")) && !(result_confirm_Q5.get(0).equals("no")) ){
                        promptSpeechInput_confirm_Q5();
                    }
                    else if (result_confirm_Q5.get(0).equals("no")){
                        promptSpeechInput_team();
                    }
                }
            }
        }
    }

编辑:我应该注意,在调试时,应用程序实际上不会提示对中间问题的响应。它只是简单地提出问题,然后继续下一个问题。

编辑:我在一个单独的活动中再次使用了这段代码,所以我知道现在的问题是代码本身。

此代码不起作用的原因是每个确认问题的末尾都没有中断。在每个if语句的末尾添加一条换行符,包含yes和nos.

最新更新