使用我的第一款Android应用程序Cat Calculator。在猫发出咕噜声之后,计算工作正常,但如果它想要猫薄荷或花哨的盛宴,它会再次从最终的“Else”声明中获得调试错误,而不是猫咬或忽略你。
提供完整的代码,但错误很可能是在最后10行......
感谢您提供的任何帮助。 -Michael
/** Called when the user clicks the CatCalculator button */
public void doCalculation(View view) {
TextView homeAnswerView = (TextView) findViewById(R.id.homeAnswerView);
TextView catActivityView = (TextView) findViewById(R.id.catActivityView);
Random randomcatmood = new Random();
double catmood = randomcatmood.nextDouble();
if (catActivityView.getText().equals("cat calculator is sleeping on calculator")) {
if (catmood <= 0.33) {
catActivityView.setText("cat calculator starts purring");
} else if (catmood <= 0.66) {
catActivityView.setText("cat calculator wants fancy feast");
} else {
catActivityView.setText("cat calculator wants a catnip toy");
}
} else if (catActivityView.getText().equals("cat calculator starts purring")) {
int answerInt;
String answer;
EditText numberOne = (EditText) findViewById(R.id.number1);
EditText numberTwo = (EditText) findViewById(R.id.number2);
int numberOnee = Integer.parseInt(numberOne.getText().toString());
int numberTwoo = Integer.parseInt(numberTwo.getText().toString());
answerInt = numberOnee * numberTwoo;
answer = Integer.toString(answerInt);
homeAnswerView.setText(answer);
} else if (catActivityView.equals("cat calculator wants fancy feast")) {
catActivityView.setText("cat calculator bites you for petting it now!");
} else if (catActivityView.equals("cat calculator wants a catnip toy")) {
catActivityView.setText("cat calculator bites you for petting it now!");
} else if (catActivityView.equals("cat calculator bites you for petting it now!")) {
catActivityView.setText("cat calculator ignores you");
} else if (catActivityView.equals("cat calculator ignores you")) {
} else {
catActivityView.setText("debug: this should never happen");
}
}
答案 0 :(得分:2)
你在后面的行中遗漏了一堆getText()
。
代码
catActivityView.equals("cat calculator wants fancy feast")
将TextView
与您提供的String
进行比较,而不是TextView
的内容。你想要这样的东西:
catActivityView.getText().equals("cat calculator wants fancy feast")
你的前两个条件是正确的。这两个之后的所有人都缺少getText()
。