-1

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)
Mike994
  • 55
  • 8
  • 1
    The second line is not an error. The first line is not related to your app. You need to examine Logcat, looking for the Java stack trace: https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – CommonsWare Jul 13 '19 at 23:28
  • If I have understood fine, I don't receive any java stack trace because in my logcat I have only those two red lines that I have wrote above, when my app crash. The compiler also doesn't show any error. – Mike994 Jul 13 '19 at 23:47
  • Mark the "If" statement please. – SnakeException Jul 13 '19 at 23:52
  • "I don't receive any java stack trace" -- if you are crashing, there will be a stack trace coming from your app. Make sure that you have the Logcat tool set to "No filters". – CommonsWare Jul 13 '19 at 23:56
  • @InsurgentPointerException I have added comment in the code where the if statement cause me the crash – Mike994 Jul 14 '19 at 00:01
  • @CommonsWare Thanks, you were right. My logcat was not set to "no filter". Now i have updated my post with the java stack trace – Mike994 Jul 14 '19 at 00:07
  • You are crashing on line 84 of `Home`. My guess is that is `textView6.text = t.toString()`, which seems to be updating a `TextView` on a background thread. – CommonsWare Jul 14 '19 at 00:11
  • @CommonsWare yes, i want to increase the values inside that textview by 1 when the progress bar reach 100. How can I fix it? – Mike994 Jul 14 '19 at 00:13
  • You could use `post()` on your `Handler`, or `runOnUiThread()`, as you are using for the rest of your `TextView` updates. – CommonsWare Jul 14 '19 at 00:14
  • @CommonsWare Thanks for your replies. I will try with the Handler, but when I use runOnUiThread() in Reward class, the app is crashing the same for updating the other textviews – Mike994 Jul 14 '19 at 00:17
  • @CommonsWare shouldnt ```` textView12.visibility = View.INVISIBLE```` also be inside the runOnUiThread...? just a question looking at the code lol – a_local_nobody Jul 14 '19 at 08:52

1 Answers1

0

I am not 100% sure but try this in your Thread block:

Replace this:

handler.post {
    mProgress.progress = pStatus
    tv2.text = pStatus.toString() + "%"
}

with this:

Handler(Looper.getMainLooper()).post {
   mProgress.progress = pStatus
   tv2.text = pStatus.toString() + "%"
}
Birju Vachhani
  • 6,072
  • 4
  • 21
  • 43