1

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().schedule(new TimerTask() {          
                @Override
                public void run() 
                {
                    resetcolor();
                    nextpage();
                    rg.clearCheck();
                    showdata();
                }
            }, 6000);

And here's my Log cat Error..

09-12 12:11:06.775: W/dalvikvm(489): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
09-12 12:11:06.789: E/AndroidRuntime(489): FATAL EXCEPTION: Timer-0
09-12 12:11:06.789: E/AndroidRuntime(489): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
09-12 12:11:06.789: E/AndroidRuntime(489):  at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
09-12 12:11:06.789: E/AndroidRuntime(489):  at android.view.ViewRoot.invalidateChild(ViewRoot.java:607)
09-12 12:11:06.789: E/AndroidRuntime(489):  at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:633)
09-12 12:11:06.789: E/AndroidRuntime(489):  at android.view.ViewGroup.invalidateChild(ViewGroup.java:2505)
09-12 12:11:06.789: E/AndroidRuntime(489):  at android.view.View.invalidate(View.java:5115)
09-12 12:11:06.789: E/AndroidRuntime(489):  at android.widget.TextView.invalidateDrawable(TextView.java:3796)
09-12 12:11:06.789: E/AndroidRuntime(489):  at android.graphics.drawable.Drawable.invalidateSelf(Drawable.java:300)
09-12 12:11:06.789: E/AndroidRuntime(489):  at android.graphics.drawable.DrawableContainer.selectDrawable(DrawableContainer.java:227)
09-12 12:11:06.789: E/AndroidRuntime(489):  at android.graphics.drawable.StateListDrawable.onStateChange(StateListDrawable.java:97)
09-12 12:11:06.789: E/AndroidRuntime(489):  at android.graphics.drawable.Drawable.setState(Drawable.java:400)
09-12 12:11:06.789: E/AndroidRuntime(489):  at android.widget.CompoundButton.drawableStateChanged(CompoundButton.java:271)
09-12 12:11:06.789: E/AndroidRuntime(489):  at android.view.View.refreshDrawableState(View.java:7248)
09-12 12:11:06.789: E/AndroidRuntime(489):  at android.widget.CompoundButton.setChecked(CompoundButton.java:115)
09-12 12:11:06.789: E/AndroidRuntime(489):  at android.widget.RadioGroup.setCheckedStateForView(RadioGroup.java:179)
09-12 12:11:06.789: E/AndroidRuntime(489):  at android.widget.RadioGroup.check(RadioGroup.java:159)
09-12 12:11:06.789: E/AndroidRuntime(489):  at android.widget.RadioGroup.clearCheck(RadioGroup.java:205)
09-12 12:11:06.789: E/AndroidRuntime(489):  at com.example.quizapp.Firstques$1$1.run(Firstques.java:234)
09-12 12:11:06.789: E/AndroidRuntime(489):  at java.util.Timer$TimerImpl.run(Timer.java:289)
4

2 回答 2

1

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= new Handler()
m_handlerTask = new Runnable()
{
    @Override 
    public void run() { 

         // do something
          resetcolor();
          nextpage();
          rg.clearCheck();
          showdata() 

         m_handler.postDelayed(m_handlerTask, 6000);    

    }
};
m_handlerTask.run();

To stop the handlerm_handler.removeCallbacks(m_handlerTask)

于 2013-09-12T07:04:21.857 回答
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").

于 2013-09-12T07:04:50.657 回答