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 with Java Basic Android Programming Accessing Views in Code: Assigning Variables

Riya Prasannan
Riya Prasannan
806 Points

Not able to assign values to factTextView. factTextview2 shows correct though, but dont have the variable factTextView2.

//assigning values to the variables factTextView = (TextView) findViewById(R.id.factTextView2);// shows correct while

//assigning values to the variables factTextView = (TextView) findViewById(R.id.factTextView);//this shows error

factTextView2 variable is not created.

Joshua Douce
Joshua Douce
13,120 Points

Hello Riya

At a quick glance, there may be a couple reasons here why you are getting an error. Here is a quick example of how your code should look

you should have a private member variable for each text view

//I would name these something different in general ending fields with a number is not best practice private factTextView;// this is the private variable that will store text view from the layout private factTextView2; // this is the private variable that will store your second text view from the layout

Just double checked your question this is probably the information your looking for :) then inside your onCreate() Method factTextView = (TextView) findViewById(R.id.factTextView);//this is looking for an id by the name factTextView on your layout factTextView2 = (TextView) findViewById(R.id.factTextView2);//this is looking for an id by the name factTextView2 on your layout

the error you are getting may be caused by not having a matching textView in your layout or that you may not have an appropriate matching private variable. I also, noticed that in your example you are assigning 2 separate text views to the same private member variable the second assignment statement will override the first. Also, copy and pasting the error message is always helpful when asking for help :)

4 Answers

Joshua Douce
Joshua Douce
13,120 Points

Hi Riya

I think the issue is that the id in your layout file must be factTextView2, not factTextView.

If you go your layout file and click on your textView. check the upper right-hand corner/properties section and ensure it says id: factTextView. Alternatively, go to your layout file and click on the text view tab. look for the android: id attribute and check that it says "facttextView"

findViewById(R.id.factTextView). what this line is doing is going in your main activity layout file an looking for a vie with the id of factTextView so it's likely that it cannot find a view with the id factTextView

Riya Prasannan
Riya Prasannan
806 Points

Hi Joshua,

Thanks for the reply. I never declared factTextView2, not sure where it came from. Below is the entire code. In here, factTextView alone shows red. When I hover the mouse near to it, it says "Cannot resolve the symbol factTextView.

import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; import android.widget.Button;

public class FunFactsActivity extends AppCompatActivity {

// declaring our View variables
private TextView factTextView;
private Button showFactButton;

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

    //assigning values to the variables
    factTextView = (TextView) findViewById(R.id.factTextView);
    showFactButton = (Button) findViewById(R.id.showFactbutton);


}

}

Riya Prasannan
Riya Prasannan
806 Points

Hi Joshua,

Thanks so much. Its working perfectly now. In the layout, id was shown as factTextView2. I changed it and its working now.

Appreciate your help.

Joshua Douce
Joshua Douce
13,120 Points

You are very welcome :D