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

android studio is telling me that it can not resolve symbol factLabel when i type factLabel.setText what did i do wrong

cant seem to figure out what i did wrong

8 Answers

william parrish
william parrish
13,774 Points

TextView factLabel = (TextView)findViewById(R.id.yourid);

william parrish
william parrish
13,774 Points

Did you cast factLabel to a textView?

The error you describe is in the definition of your variable, or its initialization.

First check for typos. Java, thus Android is a strongly typed language, so factLabel and FactLabel are intact two different variable names. Also make sure if you have types TextView correctly.

If you still have problems just paste our code here and we can have a look

how do i cast fact label to a text view

thank you guys so much i figured it out i had typed "fileLabel" earlier in the code instead of factLabel

Having same problems:

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);
}

}

william parrish
william parrish
13,774 Points

You did not declare your variable button. Before you can cast and assign a value to it from your R.id.xx it needs to be declared.

Right at the beginning of your class type

Button showFactButton;

I dont seen anything in your code named 'factTextView' but your factLabel has the same problem as your button does.

Can you be more specific? I followed the code and compared what I can see in the video. I am not sure what you mean by declaring.

william parrish
william parrish
13,774 Points

Declaring a variable would be something like this

Button mButton;

You are essentially declaring that you are going to have a button, and it will be named mButton.
Once you have done this, you can then assign it a value

mButton =(Button)findViewById(R.id.whatever);

You cant declare, and assign a value to an object on the same line usually. You can do this with primitive data types like integers, double's, booleans etc ( int x = 3; is valid for example ).

But non primitive data types require a two step process.

  1. Declare it
  2. Assign it a value.

If the first time you are using your button is in this line Button showFactButton = (Button) findViewById(R.id.showFactButton);

You are wrong.

Declare it, like i mentioned above int he previous post, right where your class starts public class FunFactActivity extends Activity { put variable declaration here

Thanks for the info. I was able to resolve the problem. I forgot to check the .xml file where the variables where declared.