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 Adding the onClick() Method

cannot find symbol variable

Problem:

Cannot resolve symbol factTextView Cannot resolve symbol showFactButton

I'm at a loss on where to check for errors.. please help.

Code:

package xStatic.funfacts;

import android.app.Activity; 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 FunFactActivity extends Activity {

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

    // Declare our View variables and assign 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 view) {
            // The 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 menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.fun_fact, 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);
}

}

4 Answers

You have given different Ids to those elements as I assumed. Change this..

    final TextView factLabel = (TextView) findViewById(R.id.factTextView);
    Button showFactButton = (Button) findViewById(R.id.showFactButton);

to

    final TextView factLabel = (TextView) findViewById(R.id.textView);
    Button showFactButton = (Button) findViewById(R.id.button);

OR change the ids in the .xml instead, let me know if it helps.

PS...I am not sure which textView displays what. I assume textView id goes to the text with the facts.

THANK YOU THANK YOU THANK YOU! I needed help on where to look.

That fixed the problem

You are welcome :)

Maybe it's just me but I don't see an error here. Could you please post your .xml file?

Post the code like this please:

//3 times the symbol under escape

//empty line

//code

//empty line

//3 times the symbol under escape

I think the problem might be you forgot to set those ids you are asking for for the fact button and the Textview in your .xml file that contains them. Try to see if those 2 items have an id in your .xml code that contains their code. If you don't get it there, then that is the problem.

//``` // <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=".FunFactActivity" android:background="#ff51b46d">

<TextView
    android:text="@string/did_know"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView2"
    android:textSize="24sp"
    android:textColor="#80ffffff" />

<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/textView"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textSize="24sp" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Show another Fun Fact"
    android:id="@+id/button"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:background="@android:color/white" />

</RelativeLayout> // //```

I hope this is the .xml you are talking about? I'm pretty new to programming and I'm not sure what xml file i need to post or if there are others.

Thanks in advance! I thought I followed the video exactly, but I could have typed something wrong.