android - java.lang.IllegalStateException: TimerTask is scheduled already -


having problem timer shows error. have timer schedules timertask (close). then, inside timertask schedule next timertask , on. understanding should work, when run app has "stopped working" error.

levelcreationtimer.schedule(close, 1000); <= called in oncreate.

timer levelcreationtimer = new timer(); timertask close = new timertask() {     @override     public void run() {         if (points < 10) {             switch (randnum.nextint(2)) {                 case 0:                     locationblimps[0][0] = 0;                     locationblimps[0][1] = 0;                     locationblimps[1][0] = -1;                     break;                 case 1:                     locationblimps[0][0] = 4;                     locationblimps[0][1] = 4;                     locationblimps[1][0] = -1;                     break;             }         }         int = 0;         while (locationblimps[i][0] != -1) {             final int final_i = i;             runonuithread(new runnable() {                 public void run() {                     sky[locationblimps[final_i][1]][locationblimps[final_i][0]].setbackgroundresource(r.drawable.scissors);                 }             });             i++;         }         levelcreationtimer.schedule(far, 1000);     } }; timertask far = new timertask() {     @override     public void run() {         int j = 0;         while (locationblimps[j][0] != -1) {             final int final_j = j;             runonuithread(new runnable() {                 public void run() {                     sky[locationblimps[final_j][1]][locationblimps[final_j][0]].setbackgroundresource(r.drawable.scissors);                     obstacles[locationblimps[final_j][1]][locationblimps[final_j][0]] = true;                 }             });             j++;         }         levelcreationtimer.schedule(last, 1000);     } }; timertask last = new timertask() {     @override     public void run() {         int k = 0;         while (locationblimps[k][0] != -1) {             final int final_k = k;             runonuithread(new runnable() {                 public void run() {                     sky[locationblimps[final_k][1]][locationblimps[final_k][0]].setbackgroundresource(r.drawable.sky);                     obstacles[locationblimps[final_k][1]][locationblimps[final_k][0]] = false;                 }             });             k++;         }         levelcreationtimer.schedule(close, 10000);     } }; 

i tried adding

        levelcreationtimer.cancel();         levelcreationtimer = new timer(); 

before each schedule in-order delete schedule, did not solve issue.

i solved problem creating classes extend timertask , schedule instances of classes.

class myclass1 extends timertask {     @override     public void run()     {         //my code         levelcreationtimer.schedule(new myclass2(),1000);         //scheduling next timertask ^     } } class myclass2 extends timertask {     @override     public void run()     {         //my code         levelcreationtimer.schedule(new myclass1(),1000);         //scheduling next timertask ^     } } 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -