Hello everyone and thank you for your intention to help me!
I'm in a beginner's course right now and this is the program I needed to write:
- Declare numerator and denominator variables
- 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
CodePudding user response:
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!
CodePudding user response:
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
}
}