Android Build a Simple Android App with Java Basic Android Programming Choosing a Random Fact

Quantavius Mills
Quantavius Mills
250 Points

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

Quantavius Mills
Quantavius Mills
250 Points

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

fatima Tavakoli
fatima Tavakoli
12,466 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

Quantavius Mills
Quantavius Mills
250 Points

Thanks Fatima!! I appreciate your feedback :)