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 trialmarcinjeden
526 Points"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
Zeus Intuivo
13,129 PointsWell, 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.
marcinjeden
526 PointsOK, 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?
Zeus Intuivo
13,129 PointsIt 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 ?
marcinjeden
526 PointsYeasterday 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.
Zeus Intuivo
13,129 PointsIf 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.
marcinjeden
526 Pointsmy id is: marcin,dlubis
thanks :)