Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is Android Studio not flagging this code line as an error?  RSS feed

 
Ranch Hand
Posts: 230
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I'm still learning and a tutorial I am following shows that the below code ( line 9 ) should have flagged for incompatible types.  The instructor's video shows the error "Incompatible types.  Required: android.widget.ImageView, Found: android.view.View".
My Android Studio however isn't showing an error.  I looked at the class file for ImageView and there isn't a findViewById() method so I think it should have returned an error.

Is there a setup issue with my Android Studio ?



Based
 
Bartender
Posts: 1301
36
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that you need to cast the result of findViewById to the type of view you want, don't you?
Wouldn't this be the correct way to do it:


Which tutorial are you following along in?
 
Pete Letkeman
Bartender
Posts: 1301
36
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But to answer your question.
findViewById() returns a generic View. There are many objects that use the View type as a base object.
ImageView is a specific type of View which has more properties then the generic View type.
Its kind of like this View, by itself does not know about the extended properties of ImageView, which is why you have to cast it or you get warnings/errors.
You can see the relationship between View and ImageView here https://developer.android.com/reference/android/widget/ImageView.html.

Hope this helps clear things up somewhat.
 
Lisa Austin
Ranch Hand
Posts: 230
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pete Letkeman wrote:I think that you need to cast the result of findViewById to the type of view you want, don't you?
Wouldn't this be the correct way to do it:


Which tutorial are you following along in?



Hi Pete,  I think I may not have been clear.   I understand the problem and that I should have received an incompatible type error.   My actual issue is that I did not receive an error.  Android Studio didn't flag it as a problem and it should have.  I want to know why Android Studio didn't flag it ( didn't give it a red squiggly line ) .
 
Rancher
Posts: 1832
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is your compile sdk version.  Since api 26 it uses Java’s generics automatic type inference and you don't need to do manual type cast.
 
Pete Letkeman
Bartender
Posts: 1301
36
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, okay. I remember something about this not being needed with newer versions in a Stack Over Flow posting.
After some Googling I was able to find this posting
https://stackoverflow.com/questions/44902651/no-need-to-cast-the-result-of-findviewbyid
Which has a link to https://www.techyourchance.com/casting-android-views/.

So it looks like you may not need to do the view casting depending on which Android SDK you are programming against, or maybe not at all.
I do think that it is pretty silly to have to cast the result of findViewById() when the compiler should be able to find that out at compile time.
 
Lisa Austin
Ranch Hand
Posts: 230
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swastik Dey wrote:What is your compile sdk version.  Since api 26 it uses Java’s generics automatic type inference and you don't need to do manual type cast.



Yup that's what I have .   compileSdkVersion 26.  Thank You
 
Lisa Austin
Ranch Hand
Posts: 230
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pete Letkeman wrote:Oh, okay. I remember something about this not being needed with newer versions in a Stack Over Flow posting.
After some Googling I was able to find this posting
https://stackoverflow.com/questions/44902651/no-need-to-cast-the-result-of-findviewbyid
Which has a link to https://www.techyourchance.com/casting-android-views/.

So it looks like you may not need to do the view casting depending on which Android SDK you are programming against, or maybe not at all.
I do think that it is pretty silly to have to cast the result of findViewById() when the compiler should be able to find that out at compile time.



Got it.  Thank You very much.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!