我这里有这个代码,旨在让用户回答一系列问题。为了确保语音输出和输入不会发生冲突,我在询问每个问题后都实施了延迟。但是,现在应用程序将直接跳过第一个问题“你在哪个城镇?”到最后一个问题“你上次参加过哪支球队?”。当我尝试调试此代码时,我发现应用程序实际上会转到提示中间问题的语音输入的函数。然而,应用程序只是跳过这些功能,并直接跳到最后一个。任何人都可以帮我找到我的代码中的错误吗?整个活动有很多代码,所以如果有任何有用的东西,请告诉我,以便发布。
感谢您的帮助:)
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();
}
}
}
}
}
编辑:我应该注意,在调试时,应用程序实际上并没有提示中间问题的响应。它只是询问问题并转到下一个问题。
编辑:我在一个单独的活动中再次使用此代码,所以我知道现在的问题是代码本身。
答案 0 :(得分:0)
此代码不起作用的原因是每个确认问题结束时没有中断。使用yes和nos在每个if语句的末尾添加一个中断行。