I’m trying to understand how to use a timer.
In my MainActivity.class, inside on create method, i have this code
Timer timer = new Timer();
TimerTask updateM = new GestioneSlide();
timer.scheduleAtFixedRate(updateM , 1000, 5000); // i want to do a thing every second for 5 seconds
then i have another class where i override the method run, and where i want to write something in a texview that is in my MainActivity.class
class GestioneSlide extends TimerTask {
@Override
public void run() {
MainActivity.TextViewName.setText("bla bla");
}
}
My app crash, if i delete the MainActivity.TextViewName.setText("bla bla"); i have no problem. Probably i can’t write in that textview that way
What did i do wrong?
The timer does not fire its actions in GUI thread. Use runOnUiThread to switch into it. Assuming GestioneSlide is the inner class of your Activity, write