Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Simple Android App (retired 2014) Pretty Little Things Adding an Image

Marcus Pierce
Marcus Pierce
8,752 Points

cannot be resolved to a variable

At the end of the first lesson of Pretty Little Things I get an error that prevents me from running the app. The error is "answer cannot be resolved to a variable" which is found in the the MainActivity. java resource where it states answerLable.setText(answer);

The (answer) is underlined and the "quick fixes" are all things that we definitely did not do when we were going through the process in the first place. I never experienced this while running the app before, and this process of adding an image didn't seem to have any thing to do with this line of code.

Any advice would be appreciated.

10 Answers

I'm surprised that doesn't throw a load of errors at you.

From before the comment about the button being clicked, that is a method in the class. Try:

public String getAnAnswer() {

before the comment and add an additional closing brace after the return answer.

Then, in your onClick method, add, just after the comment

String answer = mCrystalBall.getAnAnswer();

This declares the variable locally and called the instance method to obtain the random answer from the array.

I think that should work for you. Let me know if not!

Steve.

That error is very descriptive. If it states that the it cannot be resolved to a variable, that means one of two things: Either it was not created, or you did create it, but have it in the wrong scope. Check to see if the variable exists. After that, check if it was declared in a scope that allows it to be accessed. Last, make sure that it did not need to be passed as an argument to the method that to the section of code it is being used.

Can you post the code for your MainActivity - we might be able to spot a problem there.

Thanks,

Steve.

Marcus Pierce
Marcus Pierce
8,752 Points

Okay. I assume you mean the entire MainActivity.java file. Here it is:

package com.example.crystalball;

import java.util.Random;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    //Declare our View variables and assign them the Views from the layout file
    final TextView answerLable = (TextView) findViewById(R.id.textView1);
    Button getAnswerButton = (Button) findViewById(R.id.button1);

    getAnswerButton.setOnClickListener(new View.OnClickListener() {

        private CrystalBall mCrystalBall = new CrystalBall();

        @Override
        public void onClick(View v) {

            //Update the label with our dynamic answer
            answerLable.setText(answer);
        }
    });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

I'm going to say the same as Victor Rodriguez , above.

I can't see where answer is declared as a variable.

In my project (which is a bit down the line from where you're up to, so is quite different!) answer is defined as String answer = "" in the CrystalBall class definition. It is returned once the method getAnAnswer is called.

I've got a separate method called handleNewAnswer which has:

String answer = mCrystalBall.getAnAnswer();
mAnswerLabel.setText(answer);

You're not quite at that point yet, but you seem to be missing the declaration of the answer string variable. If you have declared it, it is out of scope of where you are needing it and so is invisible to the code.

Steve.

Marcus Pierce
Marcus Pierce
8,752 Points

I have the same String answer = "" definition in the CrystalBall class definition. I must be not understanding something. My CrystalBall.java file reads as follows:

package com.example.crystalball;

import java.util.Random;

public class CrystalBall {

    public String getAnAnswer() {
        String[] answers = {
                "It is certain",
                "It is decidedly so",
                "All signs say YES",
                "The stars are not aligned",
                "My reply is no",
                "It is doubtful",
                "Better not tell you now",
                "Concentrate and ask again",
                "Unable to answer now" };

        // The button was clicked, so update the answer label with an answer
        String answer = "";

        Random randomGenerator = new Random();
        int randomNumber = randomGenerator.nextInt(answers.length);

        answer = answers[randomNumber];

        return answer;

    }
}
Marcus Pierce
Marcus Pierce
8,752 Points

You're not going to believe this: I made all of your changes, as instructed above. It generated like 5 more errors than before. Then I went back and reversed everything so that it was exactly as how I started, and it works now. My CrystalBall class definition and MainActivity file are exactly as posted above! I really dislike not knowing what was going on, but oh well, it works.

That's bizarre! Glad it works now, though!

No problem! :-)