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 trial

Android Build a Simple Android App with Kotlin Improving Our Code Dynamically Changing the Background Color

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Error message with Views

After hacvng downloaded a patch to Build 3 of Android Studio it looks like Kotlin as gone back to the original way of assigning views to Variables?

 //assign views
            factsTextView = findViewById(R.id.factsTextView) as TextView
            showFactsButton = findViewById(R.id.showFactsButton) as Button
            relativeLayout = findViewById(R.id.relativeLayout) as RelativeLayout

causes the following error for each line

Error:(34, 30) Type inference failed: Not enough information to infer parameter T in fun <T : View!> findViewById(p0: Int): T! Please specify it explicitly.

Which i've fixed by removing the as keyword from the declarations.

//assign views
            factsTextView = findViewById(R.id.factsTextView)
            showFactsButton = findViewById(R.id.showFactsButton)
            relativeLayout = findViewById(R.id.relativeLayout)

does this make sense? I'm now using 3.0 RC2. :)

1 Answer

Ben Deitch
STAFF
Ben Deitch
Treehouse Teacher

Yep, they updated the default behavior of 'findViewById'. We talk about this change earlier on in the track right here. Though it's interesting that it can't infer the type when we're only giving it more information about the type :/

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

I may have to look again as you suggest, but yes, I thought it was odd too. I just wanted to raise it for clarification :). Thanks again for your help.