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

Derek Derek
Derek Derek
8,744 Points

factTextView

In my FunFactsActivity.java,

the factTextView is displayed in red. When I do alt + enter, I get a dropdown option of Create id value resource 'factTextView' Create field 'factTextView' Rename reference ------------- Insert App Indexing API code Move assignment to field declaration.

Which one should I choose, and what can I do to fix the code?

Below is what I have:

mFactTextView = (TextView) findViewById(R.id.factTextView);

Antariksh Sharma
Antariksh Sharma
2,715 Points

Hey,

Error is coming because it could not get reference.

Go to your main_xml file or whatever name you have given.

In that file go to TextView and check what ID name you have assigned.

Id name and the name in the java file should be same i.e factTextView.

Happy coding:)

2 Answers

Antariksh Sharma
Antariksh Sharma
2,715 Points

Hey,

Error is coming because it could not get reference.

Go to your main_xml file or whatever name you have given.

In that file go to TextView and check what ID name you have assigned.

Id name and the name in the java file should be same i.e factTextView.

Happy coding:)

check res > layout > activity_fun_facts.xml. (in android view) should have [android:id="@+id/factTextView"] inside of [TextView]

think this as selector in html&css