简体   繁体   English

java.lang.IllegalStateException:已计划TimerTask

[英]java.lang.IllegalStateException: TimerTask is scheduled already

Having a problem with the Timer where it shows this error. Timer出现问题,显示此错误。 I have a Timer that schedules a TimerTask (close). 我有一个安排TimerTaskTimer (关闭)。 Then, inside the TimerTask I schedule the next TimerTask and so on. 然后,在TimerTask内部,我安排下一个TimerTask ,依此类推。 To my understanding this should work, but when I run it the app has the "Stopped Working" error. 据我了解,这应该可行,但是当我运行它时,该应用程序出现“ Stopped Working”错误。

levelCreationTimer.schedule(close, 1000); <= This is called in the OnCreate. <=这在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 i = 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 also tried adding 我也尝试添加

        levelCreationTimer.cancel();
        levelCreationTimer = new Timer();

before each schedule in-order to delete the schedule, but it did not solve the issue. 在每个schedule之前schedule顺序删除日程表,但是并不能解决问题。

I solved the problem by creating classes that extend TimerTask and then I schedule instances of the classes. 我通过创建扩展TimerTask类解决了该问题,然后安排了这些类的实例。

class MyClass1 extends TimerTask {
    @Override
    public void run()
    {
        //my code
        levelCreationTimer.schedule(new MyClass2(),1000);
        //scheduling the next TimerTask ^
    }
}
class MyClass2 extends TimerTask {
    @Override
    public void run()
    {
        //my code
        levelCreationTimer.schedule(new MyClass1(),1000);
        //scheduling the next TimerTask ^
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM