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

Brian Haucke
Brian Haucke
13,717 Points

How 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
Seth Kroger
56,413 Points

You 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
Brian Haucke
13,717 Points

Thanks 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?

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_fun_facts)

    // Assign the views from the layout file to the corresponding variables
    factTextView!!.text = factBook.getFact() //Trying to use the get random fact method
    //findViewById(R.id.factTextView)
    showFactButton = findViewById(R.id.showFactButton)
    relativeLayout = findViewById(R.id.relativeLayout)

    showFactButton!!.setOnClickListener{
        val fact = factBook.getFact()
        val color = colorWheel.getColor()

        // Update the screen with our new fact
        factTextView!!.text = fact
        relativeLayout!!.setBackgroundColor(color)
        showFactButton!!.setTextColor(color)
    }
Seth Kroger
Seth Kroger
56,413 Points

You should keep the original code to look up the factTextView by id and put the new code after the those lookups.

    // Assign the views from the layout file to the corresponding variables
    factTextView = findViewById(R.id.factTextView)
    showFactButton = findViewById(R.id.showFactButton)
    relativeLayout = findViewById(R.id.relativeLayout)

    // set the first fact to a random one
    factTextView!!.text = factBook.getFact()
Brian Haucke
Brian Haucke
13,717 Points

Yes! That did it. Thanks, I really appreciate it!