I'm new in app development. When I run my code on emulator/phone with an android version less or equal to 7 the app is crashing. If I run the app on emulator/phone with android higher or equals to 7.1 is working without any problems. The compiler is not giving to me any problem. SDKversion is 28.
I have analyzed my code a lot and I found that the problem is caused by some "if statement". If I comment those parts of code, the app is working also on android 7.
There are 2 relevant classes that caused to me this problem. Both of these classes have a progress bar and when the progress bar reach 100, my app needs to do some actions. For making this actions I have implemented an if statement.
Home class:
package com.example.adamo3
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.support.v7.app.ActionBar
import android.support.v7.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.animation.AnimationUtils
import android.widget.ProgressBar
import android.widget.TextView
import android.widget.Toast
import com.example.myapplication.R
import kotlinx.android.synthetic.main.activity_home.*
class Home :AppCompatActivity() {
internal var pStatus = 0
private val handler = Handler()
internal lateinit var tv: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
setSupportActionBar(findViewById(R.id.toolbar))
//home navigation
//supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.displayOptions
supportActionBar?.title = null
//setting progressBar
val drawable = resources.getDrawable(R.drawable.circular)
val mProgress = findViewById<View>(R.id.progressBar) as ProgressBar
mProgress.progress = 0 // Main Progress
mProgress.secondaryProgress = 100 // Secondary Progress
mProgress.max = 100 // Maximum Progress
mProgress.progressDrawable = drawable
/* ObjectAnimator animation = ObjectAnimator.ofInt(mProgress, "progress", 0, 100);
animation.setDuration(50000);
animation.setInterpolator(new DecelerateInterpolator());
animation.start();*/
tv = findViewById<View>(R.id.tv2) as TextView
Thread(Runnable {
while (pStatus < 100) {
pStatus += 1
Thread.sleep(200)
handler.post {
mProgress.progress = pStatus
tv2.text = pStatus.toString() + "%"
}
try {
// Sleep for 200 milliseconds.
// Just to display the progress slowly
Thread.sleep(16) //thread will take approx 3 seconds to finish
} catch (e: InterruptedException) {
e.printStackTrace()
}
if(pStatus == 100) {
//if i comment this part, this class will works
var t = textView6.text.toString().toInt()
t += 1
textView6.text = t.toString()
pStatus = 0
}
}
}).start()
}
Class Reward:
package com.example.adamo3
import android.content.Intent
import android.graphics.drawable.AnimationDrawable
import android.os.Bundle
import android.os.Handler
import android.support.constraint.ConstraintLayout
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.ProgressBar
import android.widget.TextView
import android.widget.Toast
import com.example.myapplication.R
import kotlinx.android.synthetic.main.activity_home.*
import kotlinx.android.synthetic.main.activity_reward.*
class Reward : AppCompatActivity() {
private val handler = Handler()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_reward)
setSupportActionBar(findViewById(R.id.toolbar4))
//home navigation
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setTitle(null)
// val drawable = res.getDrawable(R.drawable.circular)
val mProgress = findViewById<View>(R.id.progressBar2) as ProgressBar
mProgress.progress = 0 // Main Progress
//mProgress.secondaryProgress = 100 // Secondary Progress
mProgress.max = 100 // Maximum Progress
// mProgress.progressDrawable = drawable
Thread(Runnable {
while (mProgress.progress < 100) {
mProgress.progress += 1
// Thread.sleep(1000)
handler.post {
mProgress.progress = mProgress.progress
}
try {
// Sleep for 200 milliseconds.
// Just to display the progress slowly
Thread.sleep(16) //thread will take approx 3 seconds to finish
} catch (e: InterruptedException) {
e.printStackTrace()
}
if (mProgress.progress == 33){
runOnUiThread(Runnable{
var t = textView13.text.toString().toInt()
t = (t-1)
textView13.text = t.toString()
})
}
if (mProgress.progress == 66){
runOnUiThread(Runnable{
var t = textView13.text.toString().toInt()
t = (t-1)
textView13.text = t.toString()
})
}
if (mProgress.progress == 100) {
//if i comment this part, this class will works
runOnUiThread(Runnable{
imageView6.visibility = View.VISIBLE
textView3.visibility = View.VISIBLE
textView13.visibility = View.INVISIBLE
})
textView12.visibility = View.INVISIBLE
}
}
}).start()
}
More in details, crashes are caused by "Class -> Home": when the progress bar reach 100, one value inside a textview must increase by 1 "Class -> Reward": when the progress bar reach 100, one textview should become invisible and other 2 visibile.
Errors that the logcat is showing to me are:
2019-07-14 01:06:04.884 1753-1753/? E/RichInputConnection: Unable to connect to the editor to retrieve text. 2019-07-14 01:06:04.884 1753-1753/? W/RichInputConnection: Unable to connect to the editor. Setting caps mode without knowing text.
UPDATE--> java stack trace
2019-07-14 02:03:29.391 6450-6517/com.example.adamo3 E/AndroidRuntime: FATAL
EXCEPTION: Thread-5
Process: com.example.adamo3, PID: 6450
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6855)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1075)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:5242)
at android.view.View.invalidateInternal(View.java:13574)
at android.view.View.invalidate(View.java:13538)
at android.view.View.invalidate(View.java:13522)
at android.widget.TextView.checkForRelayout(TextView.java:7346)
at android.widget.TextView.setText(TextView.java:4479)
at android.widget.TextView.setText(TextView.java:4336)
at android.widget.TextView.setText(TextView.java:4311)
at com.example.adamo3.Home$onCreate$1.run(Home.kt:84)
at java.lang.Thread.run(Thread.java:761)