0

Working on my first Android app, Cat Calculator. Calculations work fine after the cat is purring, but if it wants catnip or fancy feast petting it again reaches the debug error from the final "Else" statement instead of the cat biting or ignoring you.

Providing full code, but the error is likely in the last 10 lines...

Appreciate any help you can provide. -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");
    }
}
4

1 回答 1

2

You're missing a bunch of getText() in your latter lines.

Code like

catActivityView.equals("cat calculator wants fancy feast")

Compares the TextView to the String you give, not the content of the TextView. You want something like:

catActivityView.getText().equals("cat calculator wants fancy feast")

Your first two conditions are correct. All the ones after those two are missing getText().

于 2013-06-29T03:49:47.890 回答