• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 335
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 335
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ) .
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 335
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 335
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic