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

The Emulator Isn't Running my New Fun Fact.

I have no error, only

it won't show the next fun fact. Here's my code:

FunFactsActivity.java:
package com.example.admin.funfacts;

        import android.app.Activity;
        import android.content.DialogInterface;
        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 Activity {

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

        // Declare our View variables and assign the variables 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 view) {
                // update the fact file with 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);
    }
}
activity_fun_facts.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".FunFactsActivity"
    android:background="#ff51b46d">

    <TextView
        android:text="Did you know?"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textColor="#80ffffff"
        android:id="@+id/TextView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ants stretch when they wake up in the morning."
        android:id="@+id/factTextView"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="24sp"
        android:textColor="@android:color/white" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/showFactButton"
        android:visibility="invisible"
        android:singleLine="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="(Touch for next Fun Fact)"
        android:id="@+id/newFunFactTextView"
        android:textSize="15sp"
        android:textColor="#82ffffff"
        android:layout_below="@+id/factTextView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="82dp" />

</RelativeLayout>

Thanks!! ISAIAH S

1 Answer

Josip Dorvak
Josip Dorvak
18,126 Points

Because everytime you click the button, you are telling your program to keep the same fact

 public void onClick(View view) {
                // update the fact file with a new fact
                String fact = "Ostriches can run faster than horses.";
                factLabel.setText(fact);
            }

Your onClick listener is setting the fact text to be the same thing every time

No, I mean it's only saying 'Ants stretch when they wake up in the morning.'