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 with Java Basic Android Programming Choosing a Random Fact

Errors at the bottom of code.

I am getting a few errors at the bottom of the code. public class MainActivity<TextVie> extends AppCompatActivity { //Declare our view variables private TextView factTextView; private Button showFactButton;

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

    // Assign the Viewid from the layout file to the corresponding variables
    factTextView = findViewById(R.id.factTextView);
    showFactButton = findViewById(R. id.showFactButton);
    String[] facts;
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String[] facts  = {
                "Ants stretch when they wake up in the morning.",
                "Ostriches can run faster than horses.",
                "You are born with 300 bones; by the time you are an adult you will have 206.",
                "Olympic gold medals are actually made mostly of silver.",
                "It takes about 8 minutes for light from the Sun to reach Earth.",
                "Some bamboo plants can grow almost a meter in just one day.",
                "The state of Florida is bigger than England.",
                "Some penguins can leap 2-3 meters out of the water.",
                "On average, it takes 66 days to form a new habit.",
                "Mammoths still walked the earth when the Great Pyramid was being built." };

            }

            // The button was clicked, so update the fact TextView with a new fact
            // Randomly select a fact
            Random randomGenerator = new Random();
            int randomNumber = randomGenerator.nextInt(10);
            String fact = facts[randomNumber];

            // Update the screen with our new fact
            factTextView.setText(fact); <--- cannot resolve factTextView, (fact) unknown class
        };
    };
    showFactButton.setOnClickListener(listener); <--- cannot resolve


}

}

Hi,

Remove the semicolon from the end of the line as shown in the screenshot below and try to run the app again:

alt text

Hi Mohammed,

Thanks for answering, I removed the semicolon for the lines and it just created more errors. I am still researching this issue.

1 Answer

Fatemah Tavakoli
Fatemah Tavakoli
13,797 Points

Hi Quantavius,

Your code is fine, you just have an extra "}" in middle of your code that is causing the error. Apart from the "showFactButton.setOnClickListener(listener);" the rest of the codes are part of the View.OnClickListener, which you are closing it after your facts array with that extra "}". Thus, the rest of codes became out of the scope.

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

        // Assign the Viewid from the layout file to the corresponding variables
        factTextView = findViewById(R.id.factTextView);
        showFactButton = findViewById(R. id.showFactButton);
        String[] facts;
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String[] facts  = {
                        "Ants stretch when they wake up in the morning.",
                        "Ostriches can run faster than horses.",
                        "You are born with 300 bones; by the time you are an adult you will have 206.",
                        "Olympic gold medals are actually made mostly of silver.",
                        "It takes about 8 minutes for light from the Sun to reach Earth.",
                        "Some bamboo plants can grow almost a meter in just one day.",
                        "The state of Florida is bigger than England.",
                        "Some penguins can leap 2-3 meters out of the water.",
                        "On average, it takes 66 days to form a new habit.",
                        "Mammoths still walked the earth when the Great Pyramid was being built." };


            // The button was clicked, so update the fact TextView with a new fact
            // Randomly select a fact
            Random randomGenerator = new Random();
            int randomNumber = randomGenerator.nextInt(10);
            String fact = facts[randomNumber];

            // Update the screen with our new fact
            factTextView.setText(fact); //<--- cannot resolve factTextView, (fact) unknown class
        };
    };
    showFactButton.setOnClickListener(listener); //<--- cannot resolve

    }

}

Keep up the good work! :D

Thanks Fatima!! I appreciate your feedback :)