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 trialBrian Haucke
13,717 PointsHow do I get my Fun Facts App to show a random fact on launch?
Currently my Fun Facts android app always shows the same fact at launch "Ants stretch when they wake up in the morning". This is hard coded into the activity_fun_facts.xml.
<TextView android:text="Ants stretch when they wake up in the morning" />
I want my Fun Facts app to show one of the random facts from val facts = arrayOf() when it launches. How do I do that?
2 Answers
Seth Kroger
56,413 PointsYou do have a method to get the next random fact when the button is pressed. You could also use it before setting the button listener to get a random fact and set the fact text to that.
Brian Haucke
13,717 PointsYes! That did it. Thanks, I really appreciate it!
Brian Haucke
13,717 PointsBrian Haucke
13,717 PointsThanks Seth! I'm trying to do this, but it's causing my app to crash at launch.
Here's the error: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.brianhaucke.funfacts/com.brianhaucke.funfacts.FunFactsActivity}: kotlin.KotlinNullPointerException
Here's my code, any thoughts?
Seth Kroger
56,413 PointsSeth Kroger
56,413 PointsYou should keep the original code to look up the factTextView by id and put the new code after the those lookups.