TransWikia.com

Android Studio. App crashes after thread sleeps

Stack Overflow Asked by LFC Productions on December 9, 2021

I want to be able to change the set text of a Text View after 3 seconds have passed. To do this I have created a try catch statement and have told the thread to sleep for 3000 milliseconds before the code below is executed. Unfortunately when I run the app the program simply waits for 3 seconds and then crashes. Any help would be greatly appreciated.

    Runnable runnable3 = new Runnable() {

        @Override
        public void run() {
            TextView sup_txt3 = findViewById(R.id.sup_txt3);
            sup_txt3.setText("Ooooohhhh this is very hard. I am receiving many thoughts");


            try{
Thread.sleep(3000);
            }catch(Exception e){

            }
            sup_txt3.setText("I am sensing the letter A");

        }
    };

One Answer

I use postDelayed() for this kind of tasks

sup_txt3.postDelayed(() -> sup_txt3.setText("I am sensing the letter A", 3000);

I shorted it, the complete code would be

sup_txt3.postDelayed(new Runnable() {
   @Override
   public void run() {
      sup_txt3.setText("I am sensing the letter A");
   }
}, 3000);

Answered by Felix on December 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP