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 (2014) Basic Android Programming Adding the onClick() Method

Fabian Claudio
Fabian Claudio
4,017 Points

Fun Fact has stopped

Hey guys the app wont run...I checked the other solutions like changing styles.xml to another code but it just showed up red. Here's the code, styles.xml and manifest.xml

public class FunFactsActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fun_facts);
}
   // Declare our view variables and assign the views from our layout file
   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 view) {
          //The button was clicked, so update the fact label to show a new fact
           String fact = "Ostriches can run faster than horses";
           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.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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

Manifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="claudio.fabian.funfacts" >

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".FunFactsActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

Styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.NoActionBar">
    <!-- Customize your theme here. -->
</style>

</resources>

2 Answers

Fabian Claudio . You did the onclicklistener outside the on create method. Ill show you the difference

//This is what you posted
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fun_facts);
}
   // Declare our view variables and assign the views from our layout file
   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 view) {
          //The button was clicked, so update the fact label to show a new fact
           String fact = "Ostriches can run faster than horses";
           factLabel.setText(fact);
           showFactButton.setOnClickListener(listener);
       }
   };
//Heres what it should look like
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fun_facts);

   // Declare our view variables and assign the views from our layout file
   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 view) {
          //The button was clicked, so update the fact label to show a new fact
           String fact = "Ostriches can run faster than horses";
           factLabel.setText(fact);
           showFactButton.setOnClickListener(listener);
       }
   };
} 

Hope it helps. If you have more questions feel free to ask :)

Fabian Claudio
Fabian Claudio
4,017 Points

Wow thanks I was super angry lol solved it

Fabian Claudio . You did the onclicklistener outside the on create method. Ill show you the difference

//This is what you posted
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fun_facts);
}
   // Declare our view variables and assign the views from our layout file
   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 view) {
          //The button was clicked, so update the fact label to show a new fact
           String fact = "Ostriches can run faster than horses";
           factLabel.setText(fact);
           showFactButton.setOnClickListener(listener);
       }
   };
//Heres what it should look like
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fun_facts);

   // Declare our view variables and assign the views from our layout file
   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 view) {
          //The button was clicked, so update the fact label to show a new fact
           String fact = "Ostriches can run faster than horses";
           factLabel.setText(fact);
           showFactButton.setOnClickListener(listener);
       }
   };
} 

Hope it helps. If you have more questions feel free to ask :)