Skip to content Skip to sidebar Skip to footer

Error In Jumping To Method After Some Delay Time

I m developing a quiz based app and in this m trying to jump on some method after some delay time but unfortunately m getting some Run time Error.. Here's that code.. new Timer().s

Solution 1:

Rewrite your code as follows:

new Thread(new Runnable() {
  @Override
  public void run() {
    try {
      Thread.sleep(6000);
      runOnUiThread(new Runnable() {
        @Override
        public void run() {
          resetcolor();
          nextpage();
          rg.clearCheck();
          showdata();
        }
      });
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
    }
  }
}).start();

Your problem is that you cannot touch UI elements from background thread. The code above executes the UI changes in the main thread (also known as "UI thread").

Solution 2:

TimerTask runs on a different thread. Ui should be updated or accessed on ui thread. So use runOnUiThread

Inside the timertask run method

new Timer().schedule(new TimerTask() {          
            @Override
            public void run() 
            {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                       resetcolor();
                       nextpage();
                       rg.clearCheck();
                       showdata();
                    }
                });
            }
        }, 6000);

Or use a Handler

Handler m_handler;
Runnable m_handlerTask ;
handler= newHandler()
m_handlerTask = newRunnable()
{
    @Overridepublicvoidrun() { 

         // do somethingresetcolor();
          nextpage();
          rg.clearCheck();
          showdata() 

         m_handler.postDelayed(m_handlerTask, 6000);    

    }
};
m_handlerTask.run();

To stop the handlerm_handler.removeCallbacks(m_handlerTask)

Post a Comment for "Error In Jumping To Method After Some Delay Time"