简体   繁体   中英

Adding animation to a button in BlackBerry

I want to use animation on a Button that I created in my BlackBerry App. The animation works fine the first time when I click the button. On first click, the button starts animation (blinking). On second click the blinking stops. However, when I click the button again (third time), the blinking should start again. However, I get an error:

App Error 104 Uncaught: IllegalStateException

The code for creating the Button and adding animation is as follows:

 final Bitmap image000 = Bitmap.getBitmapResource("panic.png");
 final Bitmap image001 = Bitmap.getBitmapResource("panicon.png");

 final Timer animationTimer = new Timer();

 final BitmapField animationField = new BitmapField(image000,BitmapField.FOCUSABLE){

     protected boolean navigationClick(int status, int time)
     {
           if(flag){
                   animationTask.cancel();
                   flag=false;
           }else{
                   animationTimer.scheduleAtFixedRate(animationTask, 0, 100);
                   flag=true;
           }

        return true;
     }


 };

 animationTask = new TimerTask() {
     public void run() {
         if(counter == 0){
             animationField.setBitmap(image000);
         }
         if(counter == 1){
             animationField.setBitmap(image001);
             counter = -1;
         }

         counter++;
     }
 };


 add(animationField);

EDIT: I debugged my code and the error occurs in the loop that starts the thread. Cancelling the thread seems fine. I am lost what is the issue. Please guide.

try this -

TimerTask animationTask;
BitmapField animationField;
final Bitmap image000 = Bitmap.getBitmapResource("panic.png");
final Bitmap image001 = Bitmap.getBitmapResource("panicon.png");
final Timer animationTimer = new Timer();
animationField = new BitmapField(image000,BitmapField.FOCUSABLE){
    protected boolean navigationClick(int status, int time)
           {
             if(flag){
                 animationTask.cancel();
                 flag=false;
             }else{
                 animationTask = new TimerTask() {
                        public void run() {
                            if(counter == 0){
                                animationField.setBitmap(image000);
                            }
                            if(counter == 1){
                                animationField.setBitmap(image001);
                                counter = -1;
                            }
                    counter++;
                        }
                     };
                 animationTask.run();
                 animationTimer.scheduleAtFixedRate(animationTask, 0, 100);
                 flag=true;
             }

              return true;
           }


     };
 animationTask = new TimerTask() {
        public void run() {
            if(counter == 0){
                animationField.setBitmap(image000);
            }
            if(counter == 1){
                animationField.setBitmap(image001);
                counter = -1;
            }
counter++;
        }
     };

add(animationField);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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