Thread Problems (Koltin)

0

I'm in a situation where I'm building an android APP to keep looking at Bitcoin's quotations on the Bitcoin Marketplace site.

The APP is already working, the last thing I want to implement is the automatic query at time intervals.

The startAll () method triggers the query, throws the current data on the screen and saves some data in the database.

For this I'm using a Thread, but when I call the startAll () method inside the Thread the APP closes. I do not understand why, because this same scheme I can reproduce in IntelliJ successfully.

I even tried putting the method call inside a try / catch to see the possible error, but without success.

NOTE: I placed this class within onCreate

Is there a better way to do this?

Follow the code:

class thread() : Thread() {

        override fun run() {

            var count = 0
            while (count < 28000) {
                try{
                    startAll()
                }catch (ex:Exception){
                    errorMessage(ex.message.toString())
                }

                count++
                Thread.sleep(30000)

            }
        }
    }

    val t = thread()
    t.start()

The following error appears in the LOG

07-18 08:47:31.234 1918-1956/com.example.tjsid.cry E/AndroidRuntime: FATAL EXCEPTION: Thread-172
Process: com.example.tjsid.cry, PID: 1918
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
    at android.os.Handler.<init>(Handler.java:200)
    at android.os.Handler.<init>(Handler.java:114)
    at android.widget.Toast$TN.<init>(Toast.java:345)
    at android.widget.Toast.<init>(Toast.java:101)
    at android.widget.Toast.makeText(Toast.java:259)
    at com.example.tjsid.cry.MainActivity.errorMessage(MainActivity.kt:103)
    at com.example.tjsid.cry.MainActivity$onCreate$thread.run(MainActivity.kt:70)
07-18 08:47:31.238 579-987/system_process W/ActivityManager:   Force finishing activity com.example.tjsid.cry/.MainActivity
07-18 08:47:31.296 592-592/? E/EGL_emulation: tid 592: eglCreateSyncKHR(1215): error 0x3004 (EGL_BAD_ATTRIBUTE)
07-18 08:47:31.386 1918-1934/com.example.tjsid.cry E/Surface: getSlotFromBufferLocked: unknown buffer: 0xdf9b44d0

Implementation of startAll ()

fun startAll() {
    val basicUrl = "https://www.mercadobitcoin.net/api/"
    val urlBit = "BTC/ticker/"
    val urlLit = "LTC/ticker/"
    val urlBCash = "BCH/ticker/"
    val listViewBit = findViewById<ListView>(R.id.listaBit)
    val listViewLit = findViewById<ListView>(R.id.listaLit)
    val listViewBCash = findViewById<ListView>(R.id.listaBCash)
    val listViewDates = findViewById<ListView>(R.id.listaDate)
    get(basicUrl + urlBit)
    get(basicUrl + urlLit)
    get(basicUrl + urlBCash)
    down_bar.text = "Última atualização " + date.getAllHour()
    listViewBit.adapter = MyCustomAdapter(this, getLastPrice(urlBit, this), "bitcoin")
    listViewLit.adapter = MyCustomAdapter(this, getLastPrice(urlLit, this), "litcoin")
    listViewBCash.adapter = MyCustomAdapter(this, getLastPrice(urlBCash, this), "bcash")
    listViewDates.adapter = MyCustomAdapter(this, getLastDate(this), "date")
}
    
asked by anonymous 06.07.2018 / 18:01

1 answer

1

The problem is that you are trying to change the view from another thread that is not the main.

Run the code changes the view in a block runOnUiThread :

runOnUiThread {
    down_bar.text = "Última atualização " + date.getAllHour()
    listViewBit.adapter = MyCustomAdapter(this, getLastPrice(urlBit, this), "bitcoin")
    listViewLit.adapter = MyCustomAdapter(this, getLastPrice(urlLit, this), "litcoin")
    listViewBCash.adapter = MyCustomAdapter(this, getLastPrice(urlBCash, this), "bcash")
    listViewDates.adapter = MyCustomAdapter(this, getLastDate(this), "date")
}

It seems like your errorMessage method shows a Toast , so it would need this tbm:

runOnUiThread {
    errorMessage(ex.message.toString())
}
    
18.07.2018 / 15:33