0

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.

Nate
  • 31,017
  • 13
  • 83
  • 207
Sarah
  • 1,895
  • 2
  • 21
  • 39
  • 1
    You are trying to update UI (animationField) from another thread. Check these answers, http://stackoverflow.com/a/8954864/431639, http://supportforums.blackberry.com/t5/Java-Development/illegalStateException-when-updating-the-labelField/m-p/835999#M147132. – Rupak Feb 10 '13 at 13:13
  • Thanks for the links @Rupak, I tried the suggestions on the link but that doesnt fix the problem. 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. – Sarah Feb 10 '13 at 13:23
  • 2
    Check this answer, http://stackoverflow.com/a/1041692/431639. I think you need to create new instance of animationTask. – Rupak Feb 10 '13 at 13:34
  • possible duplicate of [Blackberry IllegalStateException error using TimerTask](http://stackoverflow.com/questions/3980854/blackberry-illegalstateexception-error-using-timertask) – Nate Feb 10 '13 at 21:58
  • 1
    Rupak is correct on both counts. You need to call `animationField.setBitmap()` on the UI thread, for example: `UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { animationField.setBitmap(image000); }};);`. He's also right that you can't **reuse** your timer this way. Each time you want to start it, instantiate a `new AnimationTask()`. The duplicate question I flagged explains both in its answers. – Nate Feb 10 '13 at 22:00

1 Answers1

1

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);
Rince Thomas
  • 4,158
  • 5
  • 25
  • 44