Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

something something @override

i have benn trying to get this code to work but there is an error message at @override and i don't understand why it is there...

public class FunFactsActivity extends AppCompatActivity {

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

    //declare our view variables and assing them the vievs from the layout file.
    final TextView factLabel = (TextView) findViewById(R.id.factLabel);
    final Button showFactButton = (Button) findViewById(R.id.showFactButton);
    final View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        // the button was clicked, so update the fact to show something new
            String fact = "";
            //randomly sellect a fact
            Random randomGenerator = new Random(); //construct a new random number generator
            int randomNumber = randomGenerator.nextInt(3);
            fact = randomNumber + "";

            if (randomNumber == 0) {
            fact = "Ants stretch when they wake up in the morning.";
            }
            else if (randomNumber == 1) {
                fact = "Otsriches run faster than horses.";
            }
            else if (randomNumber == 2) {
                fact = "Olympic gold medals are actually made mostly of silver (about 90%).";
            }

            factLabel.setText(fact);


    showFactButton.setOnClickListener(listener);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_fun_facts, 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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

5 Answers

The problem is you haven't closed the code inside your onClick() method properly with curly braces. You need two at the end, one to close the method, and one to close the anonymous class, followed by a semi-colon to complete the assignment statement. Because the method isn't closed, the compiler can't make sense of the code that comes after it.

            factLabel.setText(fact);
        }  // end of onClick()
    };  // end of View.OnClickListener

    showFactButton.setOnClickListener(listener);

@Overrid "explains" to the compiler that the following function is an override of the class parents function. The reason those functions are predefined is that your IDE knows that these are the functions that you will use to create your program within. An example would be the onCreate() function, witch will automatically be called when your app starts. The parent has this function as well, but your code overrides it. This is why it is important that you call the super.onCreate(savedInstanceState) first in your code, so that the parent will run that function first (probably to set up some stuff).

It almost worked, but there is an error at the last }. it says '}' expected, adding an } dosn't help.

opps, wrote that wrong. adding a ; dosn't help

oh, the copy-paste didn't get all of your code for some reason. now it worked!