我的程序将在几秒钟后崩溃。为什么?

时间:2011-03-16 16:05:44

标签: java java-me timer

它可以工作几秒钟然后突然崩溃。

import java.util.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class CanvasUnit extends Canvas {

    private Timer timer; 
    private TimerTaskClass task;  
    private int Top=13 , Left=48;
    private int width = getWidth();
    private int height = getHeight();
    private static int SWidth = 10;
    private static int SHeight = 10;
    private int LStep = 5;
    private int TStep = 5;

    public CanvasUnit() {
       timer = new Timer();
       task = new TimerTaskClass();
       timer.schedule(task,10); 
    }

    public void paint(Graphics g) {

        g.setColor(0,0,0);
        g.fillRect(0, 0, width, height);
        g.setColor(255,255,255);

        g.drawRect(Left, Top, SWidth, SHeight);
    }
   private class TimerTaskClass extends TimerTask{

    public final void run(){ 

      if (Left > (width - SWidth)) { 
        LStep = LStep * (-1);
      } else if (Left < 0) {
        LStep = Math.abs(LStep); 
      }
      //
      if (Top > (height - SHeight)) {
        TStep = TStep * (-1); 
      } else if (Top < 0) {
        TStep = Math.abs(TStep);  
      }

      Left = Left + LStep;
      Top = Top + TStep;

      repaint();

      //Run the timer agian
      timer = new Timer();
      task = new TimerTaskClass();
      timer.schedule(task,10);
    }

   }
}

2 个答案:

答案 0 :(得分:1)

我认为问题是堆栈溢出,删除:

   //Run the timer agian
   timer = new Timer();
   task = new TimerTaskClass();
   timer.schedule(task,10);

来自run方法,因为它会产生一个无限循环。

答案 1 :(得分:1)

抱歉,我之前的评论不正确,因为我以为你在使用LWUIT,不知道我从哪里得到这个想法。你应该使用与此相似的东西

import javax.microedition.lcdui.*;

public class CanvasUnit extends Canvas {

    private int Top = 13, Left = 48;
    private int width = getWidth();
    private int height = getHeight();
    private static int SWidth = 10;
    private static int SHeight = 10;
    private int LStep = 5;
    private int TStep = 5;

    public CanvasUnit() {
        Thread t = new Thread(new Runnable() {

            public void run() {
                while (true) {

                    if (Left > (width - SWidth)) {
                        LStep = LStep * (-1);
                    } else if (Left < 0) {
                        LStep = Math.abs(LStep);
                    }
                    //
                    if (Top > (height - SHeight)) {
                        TStep = TStep * (-1);
                    } else if (Top < 0) {
                        TStep = Math.abs(TStep);
                    }

                    Left = Left + LStep;
                    Top = Top + TStep;

                    repaint();

                    try {
                        Thread.sleep(10);

                    } catch (Exception ex) {
                    }
                }
            }
        });
        t.start();
    }

    public void paint(Graphics g) {

        g.setColor(0, 0, 0);
        g.fillRect(0, 0, width, height);
        g.setColor(255, 255, 255);

        g.drawRect(Left, Top, SWidth, SHeight);
    }

}