Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Simple Android App (retired 2014) Getting Started with Android Accessing Views in Code

"add cast to TextView"

Hello everyone,

I did just like in that example, I have the code line: TextView answerLabel = findViewById(R.id.textView1);

but I can not find an option: "add cast to TextView". What is the problem, anyone knows? And how to fix it?

And also when I create new Android Project, it starts with activity_main.xml and fragment_mail.xml. What is the reason of that and how to make one file from those 2 xml files?

I did: projest - clean, but it didn't help anything. :(

6 Answers

Well, casting means that you need to change the object to another object. In Java, everything is an object. from your question

TextView answerLabel = findViewById(R.id.textView1);

If it is asking you to add a cast. Usually eclipse will give the option to automatically cast it. consider the following:

String hello = "hello";

which I think is correct.

If you say

String hello = 0;

it might ask for a cast.

We can cast to a string like this

String hello = (String) 0;

or

String hello = ""+0;

both ways should work.

So in your question

TextView answerLabel = findViewById(R.id.textView1);

you might need something like

TextView answerLabel = (TextView) findViewById(R.id.textView1);

or if the problem is inside the findViewById() you might need to cast inside it instead.

TextView answerLabel = findViewById((String)R.id.textView1);

Since I have not seen your code I am guessing here, I wanted to know if you get the concept of what I saying. Apples to Apples and Bananas to Bananas, when in object matters.

Also if Apples and Bananas are children of the class Fruit. and the receiving object needs a Fruit class, then you can say

Fruit fruit = new Apple();

that should also work.

OK, I underestand it now what it is.

My code was the same like in the video - just one text and one button, but I couldn't add the cast. Now I know hot to change it: ctrl + shift + o

Is there any other way to solve the problem like mine?

It would be better if I can look at your code to see what is it what you are talking about. Can you post it somewhere like in github, or can you a a remote desktop session ?

Yeasterday I spent the whole day, but stil I can not use github. Maybe will be easier with using remote desktop session? But I don't know what should I put in the field: "computer"? I didn't do it before.

If you have skype, we can start there. Send me your skype id. Then I can guide you to all this one step at a time.

my id is: marcin,dlubis

thanks :)