0

im taking this kotlin basics in google and im in the "intro to debugging" section.. total noob here and i've managed to plow through the subjects and now im stuck here.. so the expected outcome is for the textview to display a quotient every three seconds but when i run the app, it shows a static value of 60.

private val TAG = "MainActivity"

MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    Log.d (TAG, "this is where the app crashed before")
    setContentView(R.layout.activity_main)

    Log.d (TAG, "this should be logged if the bug is fixed.")
    logging()
    division()
}

fun division (){
    val numerator = 60
    var denominator = 4
    repeat(4) {
        Thread.sleep(3)
        findViewById<TextView>(R.id.division_textview).setText("${numerator/denominator}")
        Log.v(TAG, "${numerator / denominator}")
        denominator --
    }
}

been wanting to study programming/developing since i was a kid.. unfortunately, things didn't work out .. now im here and im 100% committed to this.. thanks.

1 Answers1

0

That codelab is starting to haunt me - it's not you, their code is broken and 100% wrong for this situation. I've answered this in more detail here (and that answer links to another answer with some suggestions for how to actually implement it).

Basically the problem is you're sleeping the main thread, which stops the app from running, including UI updates and reacting to touches. It won't be able to continue running until your loop finishes and exits the division function - at which point the UI can update, and finally display the current contents of the TextView, which at this point is the final result.

To the user, it just looks like nothing happens for a while and then the end result appears. This code is supposed to be part of a demonstration of recording a video of an app as well, I don't think anyone actually ran the thing. And you should never block the main thread like this. I see so many questions about this codelab from confused beginners trying to learn, for an official resource there's no excuse for it really

cactustictacs
  • 17,935
  • 2
  • 14
  • 25
  • thank you so much! i appreciate ur quick feedback.. yeah i followed the instructions step-by-step and i thought im doing something wrong. you're right.. this codelab is supposed to demonstrate how to record.. didn't realize it's going to throw in some code that im not familiar with so i felt dumb for a while lol.. anyway, i guess it's part of self-teaching journey.. i'll have a look at the link you posted. thank you again. – the_inevitable Jul 23 '22 at 22:50
  • @typing_away well I mentioned the recording thing because this code will literally freeze the display and won't show it updating, so there's nothing to record a video of! It's just a bizarre codelab, the others I've seen have been fine but this one does some fundamentally wrong things and I just wonder who signed off on it – cactustictacs Jul 24 '22 at 03:23