我的Android应用程序有一个奇怪的问题。运行上述方法时answer_str = "When the activity is destroyed"
和questCHoiceStr = "When the user leave the activity."
。但if条件返回true表示真的很奇怪!
private QuestionChoice getSelectedQuestionChoice()
{
int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton)radioButtonGroup.findViewById(radioButtonID);
if (radioButton != null)
{
String answer_str = radioButton.getText().toString();
for (int i = 0; i < map.get(current_question).size(); i++)
{
List<QuestionChoice> questionChoiceList = map.get(current_question);
QuestionChoice questionChoice = questionChoiceList.get(i);
String questCHoiceStr = questionChoice.choice;
if (questCHoiceStr.equals(answer_str));
{
return map.get(current_question).get(i);
}
}
return null;
}
else
return null;
}
我尝试添加trim()
但没有改变......
我的程序中使用的字符串是来自xml文件的字符串(也许它与此有关)。
你知道是什么原因引起的吗?
THX
答案 0 :(得分:4)
当你添加;
时,就会立即关闭。
if (questCHoiceStr.equals(answer_str)); <--
当你立即关闭它时,你的if是不是真的有效。
应该是
if (questCHoiceStr.equals(answer_str))
您当前的代码等于
if (questCHoiceStr.equals(answer_str)){
}
{
return map.get(current_question).get(i);
}
为了避免这些问题,我强迫我的朋友始终使用{}
:
答案 1 :(得分:1)
尝试删除if语句中的分号。分号标记if语句的结尾,因此大括号{}之间的下一个块只是将始终运行的代码。
if (questCHoiceStr.equals(answer_str))
{
return map.get(current_question).get(i);
}
答案 2 :(得分:0)
private QuestionChoice getSelectedQuestionChoice()
{
int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton)radioButtonGroup.findViewById(radioButtonID);
if (radioButton != null)
{
String answer_str = radioButton.getText().toString();
for (int i = 0; i < map.get(current_question).size(); i++)
{
List<QuestionChoice> questionChoiceList = map.get(current_question);
QuestionChoice questionChoice = questionChoiceList.get(i);
String questCHoiceStr = questionChoice.choice;
**if (questCHoiceStr.equals(answer_str))**
{
return map.get(current_question).get(i);
}
}
return null;
}
else
return null;
}