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

Android Build a Simple Android App (2014) Basic Android Programming Making a Button Do Something

Keep on getting errors about my code

This is my code package com.benleal.funfacts;

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

public class FunFactsActivity extends ActionBarActivity {

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

    // Declare our View variables and assign them the Views from the layout file
   final TextView factLabel = (TextView) findViewById(R.id.factTextView);
    Button showFactButton = (Button) findViewById(R.id.showFactButton);
    View.OnClickListener listener  = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             //Button was clicked, so update the fact label with a new fact
            String fact = "Ostriches can run faster than horses.";
            factLabel.setText(fact);
        }
    };
   showFactButton.setOnClickListener(listener);

@Override
public boolean onCreateOptionsMenu(Menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.funfacts,menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem) {
    // 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();
}

}

1 Answer

Hello,

It looks like you are at least missing a closing brace at the end of your onCreate function.

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

public class FunFactsActivity extends ActionBarActivity {

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

    // Declare our View variables and assign them the Views from the layout file
   final TextView factLabel = (TextView) findViewById(R.id.factTextView);
    Button showFactButton = (Button) findViewById(R.id.showFactButton);
    View.OnClickListener listener  = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             //Button was clicked, so update the fact label with a new fact
            String fact = "Ostriches can run faster than horses.";
            factLabel.setText(fact);
        }
    };
   showFactButton.setOnClickListener(listener);

}  // <-----This one appears to be missing

@Override
public boolean onCreateOptionsMenu(Menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.funfacts,menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem) {
    // 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();
}

}

That's just a quick glance. If there are any more errors that you are having, could you also give the error messages you are receiving?

Thank you! I was so confused, this seemed to solve all of my problems! Thanks so much!

You're welcome. One thing to remember for the future is that every opening {, needs a closing }. Have fun with Android development.