1

This is the program I needed to write:

  1. Declare numerator and denominator variables
  2. Start a loop: a) Wait for 3000 milliseconds b) Replace the text in the TextView (id: division_TextView) with the result of this: numerator / denominator c) Reduce denominator by 1

And this is what I got:

package com.example.debugging

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.TextView

private const val TAG = "MainActivity"

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        division()
    }

    private fun division() {
        val numerator = 60
        val denominator = 4
        repeat(4) {
            Thread.sleep(3000)
            Log.d(TAG, "Text ${findViewById<TextView>(R.id.division_TextView).text} was replaced by ${numerator / denominator}")
            findViewById<TextView>(R.id.division_TextView).text = "${numerator / denominator}"
            denominator--
        }
    }
}

So, the problem: after starting the application, it hangs with a blank white screen. And only after about 12 seconds it shows the number 60 (i.e. it just ran the last time and replaced the text with 60).

"There is a logical explanation for this, because I run devision() from onCreate i.e. even before my application was visible to the user" I thought and put devision in onResume (i.e. I wanted the loop with Thread.sleep to start executing after the application became visible to the user). This is what my code looked like after these edits:

package com.example.debugging

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.TextView

private const val TAG = "MainActivity"

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    override fun onResume() {
        super.onResume()
        division()
        Log.i(TAG, "onResume started.")
    }

    private fun division() {
        val numerator = 60
        var denominator = 4
        repeat(4) {
            Thread.sleep(3000)
            Log.d(TAG, "Text ${findViewById<TextView>(R.id.division_TextView).text} was replaced by ${numerator / denominator}")
            findViewById<TextView>(R.id.division_TextView).text = "${numerator / denominator}"
            denominator--
        }
    }
}

However, the situation has not changed at all, everything is the same.

Below I added screenshots of the application launch and the startup log (of my second code).

D/MainActivity: Text Hello World! was replaced by 15
W/MIUIScout App: Enter APP_SCOUT_WARNING State
W/MIUIScout App: Event:APP_SCOUT_WARNING Thread:main backtrace:
        at java.lang.Thread.sleep(Native Method)
        at java.lang.Thread.sleep(Thread.java:451)
        at java.lang.Thread.sleep(Thread.java:356)
        at com.example.debugging.MainActivity.division(MainActivity.kt:26)
        at com.example.debugging.MainActivity.onResume(MainActivity.kt:18)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1476)
        at android.app.Activity.performResume(Activity.java:8354)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4909)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4952)
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:54)
        at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2318)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:210)
        at android.os.Looper.loop(Looper.java:299)
        at android.app.ActivityThread.main(ActivityThread.java:8298)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:576)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1073)
I/ample.debuggin: Thread[6,tid=27409,WaitingInMainSignalCatcherLoop,Thread*=0xb40000789d63d000,peer=0x12cc19b8,"Signal Catcher"]: reacting to signal 3
I/ample.debuggin: 
I/ample.debuggin: Wrote stack traces to tombstoned
D/MainActivity: Text 15 was replaced by 20
W/MIUIScout App: Enter APP_SCOUT_HANG state
W/MIUIScout App: Event:APP_SCOUT_HANG Thread:main backtrace:
        at java.lang.Thread.sleep(Native Method)
        at java.lang.Thread.sleep(Thread.java:451)
        at java.lang.Thread.sleep(Thread.java:356)
        at com.example.debugging.MainActivity.division(MainActivity.kt:26)
        at com.example.debugging.MainActivity.onResume(MainActivity.kt:18)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1476)
        at android.app.Activity.performResume(Activity.java:8354)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4909)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4952)
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:54)
        at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2318)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:210)
        at android.os.Looper.loop(Looper.java:299)
        at android.app.ActivityThread.main(ActivityThread.java:8298)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:576)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1073)
I/ample.debuggin: Thread[6,tid=27409,WaitingInMainSignalCatcherLoop,Thread*=0xb40000789d63d000,peer=0x12cc19b8,"Signal Catcher"]: reacting to signal 3
I/ample.debuggin: 
I/ample.debuggin: Wrote stack traces to tombstoned
D/MainActivity: Text 20 was replaced by 30
D/MainActivity: Text 30 was replaced by 60
I/MainActivity: onResume started.
W/Activity: PerfMonitor: Slow Operation: Activity com.example.debugging/.MainActivity onResume took 12005ms
I/SurfaceFactory: [static] sSurfaceFactory = com.mediatek.view.impl.SurfaceFactoryImpl@a9c3d69
I/MsyncFactory: [static] sMsyncFactory = com.mediatek.view.impl.MsyncFactoryImpl@77253ee
D/ViewRootImpl[MainActivity]: hardware acceleration = true, sRendererEnabled = true, forceHwAccelerated = false
D/libMEOW: meow new tls: 0xb4000078335a07c0
D/libMEOW: applied 1 plugins for [com.example.debugging]:
D/libMEOW:   plugin 1: [libMEOW_gift.so]:
I/ample.debuggin: Thread[6,tid=27409,WaitingInMainSignalCatcherLoop,Thread*=0xb40000789d63d000,peer=0x12cc19b8,"Signal Catcher"]: reacting to signal 3
I/ample.debuggin: 
W/Looper: PerfMonitor longMsg : seq=4 plan=19:07:58.191 late=202ms wall=12318ms running=0ms h=android.app.ActivityThread$H w=159 procState=-1
W/Looper: PerfMonitor looperActivity : package=com.example.debugging/.MainActivity time=1ms latency=12533ms running=0ms  procState=-1  historyMsgCount=4 (msgIndex=1 wall=57ms seq=1 late=10ms h=android.app.ActivityThread$H w=162) (msgIndex=3 wall=144ms seq=3 late=60ms h=android.app.ActivityThread$H w=110) (msgIndex=4 wall=12318ms seq=4 late=202ms h=android.app.ActivityThread$H w=159)
I/ample.debuggin: Wrote stack traces to tombstoned

first 12 sec of an app launch after 12 seconds have passed

Dharman
  • 30,962
  • 25
  • 85
  • 135
Iurii Mg
  • 63
  • 7
  • `Thread.sleep()`, if you don’t use it on a background thread, means, completely freeze my app for this amount of time and risk having it crash with the Application Not Responding error. Read up about how to use `runOnUiThread()` instead. – Tenfour04 Aug 23 '22 at 18:27
  • @Tenfour04 it's that damn (official) codelab example again – cactustictacs Aug 23 '22 at 19:03
  • 1
    @cactustictacs That's not the only codelab with egregious code in it, but maybe it's the worst. – Tenfour04 Aug 23 '22 at 19:48
  • 1
    @Tenfour04 it's just amazing that it not only tells you to do the ultimate no-no, you're meant to be recording a video while preventing the display from updating! It's like a prank tutorial or something – cactustictacs Aug 23 '22 at 19:53

2 Answers2

1

I'm just gonna link to another answer I posted about this, which links to other answers because this comes up way too often. It's not you, the code on the official tutorial you're following is completely broken, and they're teaching you to so something you fundamentally should not do (and which means the example does not work).

If you have any questions though feel free!

cactustictacs
  • 17,935
  • 2
  • 14
  • 25
  • Thank you so much for your reply! I already thought I was doing something completely wrong... And sorry for asking the same question again, I guess I still need to learn how to use google :) – Iurii Mg Aug 24 '22 at 17:05
  • @IuriiMg oh hey don't worry about it, it's not your fault at all! You're following an official tutorial, doing exactly what they tell you to do, and it doesn't work. I just wish they'd fix it, it pops up regularly enough on here that it's obviously causing learners problems – cactustictacs Aug 24 '22 at 18:05
0

as i learned thread.sleep is not a good option for this kind of operations, that s why you are not able to see the result at every step. Could you try it with runnables. I wrote a sample for you, i have a start button and a textView to see the result.

class MainActivity : AppCompatActivity() {


var runnable : Runnable = Runnable {  }
var handler : Handler = Handler(Looper.getMainLooper())

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


fun start(view: View) {

    val numerator = 60
    var denominator = 4

    runnable = object : Runnable {
        override fun run() {
            println("${numerator/denominator}")
            textView.text=(numerator/denominator).toString()
            handler.postDelayed(this,3000)
            denominator--
        }

    }

    handler.post(runnable)

    button.isEnabled = false


}

}

Ugur
  • 32
  • 5
  • When you learn more about kotlin, you can use coroutines to not lock the main thread. – Ugur Aug 23 '22 at 17:23
  • Thank you very much for your reply, I will definitely try to solve this problem the way you described :) – Iurii Mg Aug 24 '22 at 17:06