This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Hands On Software Engineering with Python and have Brian Allbey on-line!
See this thread for details.
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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

 
Ranch Hand
Posts: 252
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: 1868
81
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: 1868
81
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: 252
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: 1920
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: 1868
81
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: 252
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: 252
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.
 
What's gotten into you? Could it be this tiny ad?
ScroogeXHTML 8.0 - RTF to HTML5 and XHTML converter
https://coderanch.com/t/704747/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!