我有一些文本视图,我想检查它们并返回第一个为空的。我已经设法做到了:
public TextView CurrentPos(){
if (!wordLetter1.getText().toString().equals("*") &&
!wordLetter2.getText().toString().equals("*") &&
!wordLetter3.getText().toString().equals("*") &&
!wordLetter4.getText().toString().equals("*") &&
!wordLetter5.getText().toString().equals("*") &&
!wordLetter6.getText().toString().equals("*") &&
!wordLetter7.getText().toString().equals("*")){
if (wordLetter1.getText().toString().equals("") &&
wordLetter2.getText().toString().equals("") &&
wordLetter3.getText().toString().equals("") &&
wordLetter4.getText().toString().equals("") &&
wordLetter5.getText().toString().equals("") &&
wordLetter6.getText().toString().equals("") &&
wordLetter7.getText().toString().equals(""))
return wordLetter1;
else if (!wordLetter1.getText().toString().equals("") &&
wordLetter2.getText().toString().equals("") &&
wordLetter3.getText().toString().equals("") &&
wordLetter4.getText().toString().equals("") &&
wordLetter5.getText().toString().equals("") &&
wordLetter6.getText().toString().equals("") &&
wordLetter7.getText().toString().equals(""))
return wordLetter2;
else if (!wordLetter1.getText().toString().equals("") &&
!wordLetter2.getText().toString().equals("") &&
wordLetter3.getText().toString().equals("") &&
wordLetter4.getText().toString().equals("") &&
wordLetter5.getText().toString().equals("") &&
wordLetter6.getText().toString().equals("") &&
wordLetter7.getText().toString().equals(""))
return wordLetter3;
else if (!wordLetter1.getText().toString().equals("") &&
!wordLetter2.getText().toString().equals("") &&
!wordLetter3.getText().toString().equals("") &&
wordLetter4.getText().toString().equals("") &&
wordLetter5.getText().toString().equals("") &&
wordLetter6.getText().toString().equals("") &&
wordLetter7.getText().toString().equals(""))
return wordLetter4;
else if (!wordLetter1.getText().toString().equals("") &&
!wordLetter2.getText().toString().equals("") &&
!wordLetter3.getText().toString().equals("") &&
!wordLetter4.getText().toString().equals("") &&
wordLetter5.getText().toString().equals("") &&
wordLetter6.getText().toString().equals("") &&
wordLetter7.getText().toString().equals(""))
return wordLetter5;
else if (!wordLetter1.getText().toString().equals("") &&
!wordLetter2.getText().toString().equals("") &&
!wordLetter3.getText().toString().equals("") &&
!wordLetter4.getText().toString().equals("") &&
!wordLetter5.getText().toString().equals("") &&
wordLetter6.getText().toString().equals("") &&
wordLetter7.getText().toString().equals(""))
return wordLetter6;
else if (!wordLetter1.getText().toString().equals("") &&
!wordLetter2.getText().toString().equals("") &&
!wordLetter3.getText().toString().equals("") &&
!wordLetter4.getText().toString().equals("") &&
!wordLetter5.getText().toString().equals("") &&
!wordLetter6.getText().toString().equals("") &&
wordLetter7.getText().toString().equals(""))
return wordLetter7;
}
return null;
}
以上工作正常,但我认为这必须是一种更兼容的方式,可以用更少的代码行来实现相同的目标。我对java没有太多经验,我找到了一些类似情况的答案,我不能完全理解。我尝试使用上面的代码减少 CurrentPos() 方法的代码:
public TextView CurrentPos(){
TextView wordletters[] = {wordLetter1, wordLetter2, wordLetter3,
wordLetter4, wordLetter5, wordLetter6, wordLetter7};
TextView currentPos = null;
for(int i=0; i<wordletters.length; i++){
if(!wordletters[i].toString().equals(("*")) && wordletters[i].equals("")){
currentPos = wordletters[i];
return currentPos;
}
}
return null;
}
这给出了错误 NulPointerException。显然我做错了什么,我不明白。我的代码是完全错误的还是只是缺少了什么?