Correct_toast & incorrect_toast variable errors

The code is typed exactly like the text up to page 45. When it is ran three errors appear.

This is in regards to the code in page 44 here it is in highlighted in red

private void checkAnswer( boolean userPressedTrue){
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();

    int messageResId = 0;

    if (userPressedTrue == answerIsTrue) {
        messageResId = R.string.correct_toast;
    }else {
        messageResId = R.string.incorrect_toast;
    }

    Toast.makeText(this, messageResId, Toast.LENGTH_SHORT)
            .show();
}

I looked throughout the previous code in the book and could not find any mention to the variables incorrect_toast and correct_toast.
So I used the shortcut tool
alt enter
to correct it

it added the following

public static int correct_toast;
public static int incorrect_toast;

to R.java
This fixed the issue, however when the program was run the same errors occurred again and the code from R.java that was generated from pressing alt enter was deleted and I was back at square one.

I’m not sure if those vars are missing from the book or not, however, Android Studio mis-lead you a bit with adding those variables. It’s attempting to guess how to help, but in this case it guessed wrong.

Here’s how to fix it.
Open the strings.xml file under resources\values in your project. Here’s what it will look like, though mine is a different project.

Now just add those two new strings:
<string name=“correct_toast”>Correct</string>
<string name=“incorrect_toast”>Incorrect</string>

After you make that change :

  1. delete* those variables that Android studio added for you
  2. rebuild

That should fix it. These are String resources that are supposed to be defined separately.

EDIT
*Actually I’m not sure if you’ll need to delete the vars because you shouldn’t really edit the R.java by hand. First, try just rebuilding after altering your strings.xml.

1 Like

Hey thanks that completely fixed the problem. Much Props!!

1 Like

Hi guys I’m new to Java and was wondering as to how we can assign a string to the messageResId variable although its initialized as an integer ? Thanks in advance!