Search code examples
androidvoice

Why is does the app skip over a few questions in this line of code?


I have this code here that aims to have the user answer a series of questions. To make sure that the voice output and input don't clash, I implemented a delay after each question was asked. However, now the app will skip straight from the first question "what town are you in?" to the last question "what team did you last play?". When I attempted to debug this code, I found that the app does in fact go to the functions that prompt the speech inputs for the middle questions. However, the app simply skips over these functions, and jumps straight to the last one. Can anybody help me find the bug in my code? There is a lot more code to this entire activity, so if there is anything that will be helpful, please let me know so I can post it.

Thanks for the help :)

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();
                    }
                }
            }
        }
    }

Edit: I should note that when debugging, the app does not actually prompt a response for the middle questions. It simply asks the questions and moves on to the next one.

Edit: I used this code again in a separate activity, so I know that the issue is the code itself now.


Solution

  • The reason this code doesn't work is that there aren't breaks at the end of each confirm question. Add a break line at the end of each if statement with the yes and nos.