2

I am making a project in Aandroid, in which I need to enable and disable more than one timer again and again. To restart a timer I tried timer1.cancel(), timer1.purge, timer2.schedule etc. and also wait. But each time the program is crashing. There is no syntax error. Here is the sample:

foc.setOnClickListener(new View.OnClickListener() {

 @Override
 public void onClick(View v) {

  if(col==1)
  {
   t.cancel();
   t.purge();
   songt=new Timer();
   songt.schedule(songtask, 1000, mdl.getspeed());
  }
}
});  


b[len].setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg) {
                        songt.cancel();
                        songt.purge();
                        //t=new Timer();
                        t.scheduleAtFixedRate(ttask, 1000, mdl.getspeed());
                    }
                });  

public TimerTask ttask=new TimerTask() {

    @Override
    public void run() {
                runOnUiThread(new Runnable()
                {
                    public void run()
                    { 
                        foc.requestFocus();
                        if(col==1)
                        {
                            lay.setBackgroundColor(Color.TRANSPARENT);
                            op.setBackgroundColor(Color.CYAN);
                        }
                        else
                        {
                            op.setBackgroundColor(Color.TRANSPARENT);
                            lay.setBackgroundColor(Color.CYAN);
                        }
                        col=1-col;
                    }
                });
    }
};


public TimerTask songtask=new TimerTask() {

    @Override
    public void run() {
        runOnUiThread(new Runnable()
        {
            public void run()
            { 
                b[in].requestFocus();
                if(rev==0)
                {
                    in++;
                    if(in==len) 
                        in=0;
                }
                else if(rev==1)
                {
                    in--;
                    if(in==-1) in=len-1;
                }
            }
        });

    }
};
Lennart
  • 9,657
  • 16
  • 68
  • 84
  • take a look here: http://stackoverflow.com/questions/2098642/pausing-stopping-and-starting-resuming-java-timertask-continuously – Tala Jul 09 '13 at 11:55

0 Answers0