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

Harrison Court
Harrison Court
4,232 Points

Cannot find symbol var showFactButton

Hello! I am having a massive problem with Android Studio. I can't get my showFactButton to work.

Code:

MainActivity.java

public class MainActivity extends AppCompatActivity {
    // Declare our View var
    private TextView mFactTextView;
    private Button mShowFactButton; // this tells me "private feild "mShowFactButton" is assigned but never accessed"


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

        // Assign the Views from the layout file to the corsis var
        mFactTextView = (TextView) findViewById(R.id.factTextView);
        mShowFactButton = (Button) findViewById(R.id.showFactButtton); // This tells me "Cannot find symbol showFactButton"

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String[] facts = {
                        "Butterflies cannot fly if their body temperature is less than 86 degrees.",
                        "Neurons multiply at a rate 250,000 neurons per minute during early pregnancy.",
                        "Elephants have the longest pregnancy in the animal kingdom at 22 months. The longest human pregnancy on record is 17 months, 11 days.",
                        "Flies jump backwards during takeoff.",
                        "The human brain is about 75% water.",
                        "A housefly will regurgitate its food and eat it again.",
                        "Termites outweigh humans by almost ten to one.",
                        "It is impossible to lick your elbow.",
                        "Intelligent people have more zinc and copper in their hair.",
                        "In every episode of Seinfeld there is a Superman somewhere.",
                        "Wearing headphones for just an hour will increase the bacteria in your ear by 700 times.",
                        "This app was made via treehouse."

                };


                String fact = "";
                // Randomly select a fact.
                Random randomGenerator = new Random();
                int randomNumber = randomGenerator.nextInt(facts.length);
                fact = facts[randomNumber];

                // Update the screen with our dynamic fact.
                mFactTextView.setText(fact);
            }
        };
    }
}

activity_fun_facts.xml

    <TextView
        android:text="Did you know?"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="28dp"
        android:id="@+id/textView2"
        android:layout_marginTop="42dp"
        android:layout_marginLeft="42dp"
        android:layout_marginStart="42dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Another Fact"
        android:id="@+id/showFactButton"
        android:background="#ffffff"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="70dp"
        android:layout_alignParentLeft="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Ants stretch when they wake up!"
        android:id="@+id/factTextView"
        android:textSize="30dp"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"/>
</RelativeLayout>

3 Answers

Ben Deitch
STAFF
Ben Deitch
Treehouse Teacher

Hey Harry! It looks like you're spelling 'button' with 3 t's: R.id.showFactButtton :)

inventor02
inventor02
2,027 Points
var private TextView mFactTextView;

There's your problem. var is used in JavaScript, not Java (which is what Android is using). Your code should work fine if you just remove the var from the start of the line.

Also, just a pointer: wrap all your code in ``` (three backticks) for easier reading and better formatting. :)

Harrison Court
Harrison Court
4,232 Points

I didn't find that. I think you found the comment. Also, that's not the button... I also forgot to say that the mShowFactButton is grayed out.

 private Button mShowFactButton;

(I have re-done the code to look a little neater, maybe you can find it this time?)

inventor02
inventor02
2,027 Points

Argh, not on top of my game there (sorry about that)! I'll take another look for ya. Would it be possible to get the line numbers of the error and if any, a stack trace?

Harrison Court
Harrison Court
4,232 Points

I'll try to add a stack trace and a line number, but for now I added comments where the errors are.