Search code examples
androidhandlerrunnable

Stop Watch throwing an error in handling the runnable and a handler


I have been working on making an efficient stop watch, that displays the instantaneous time, using a runnable and a handler. The piece of code, and the log cat is attached below. The program just functions once, and then it crashes, as soon as I press the STOP button. Even pressing the reset button calls for the malfunctioning of the program. Can someone please help me out ? CODE:

case R.id.stop:
        try {
            myTimer.wait();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        stop1 = System.currentTimeMillis();
        long ans1 = stop1 - start1;
        int milli1 = (int)(ans1 % 1000);
        int millis1 = (int)(milli1/100);
        int seconds1 = (int)(ans1/1000);
        int minutes1 = (seconds1/60);

        result.setText(String.format("%d:%02d:%d", minutes1, seconds1, millis1));   

 private void UpdateGUI() {
     //tv.setText(String.valueOf(i));
      myHandler.post(myRunnable);
   }

   final Runnable myRunnable = new Runnable() {
      public void run() {
          stop1 = System.currentTimeMillis();
            long ans1 = stop1 - start1;
            int milli1 = (int)(ans1 % 1000);
            int millis1 = (int)(milli1/100);
            int seconds1 = (int)(ans1/1000);
            int minutes1 = (seconds1/60);
            result.setText(String.format("%d:%02d:%d", minutes1, seconds1, millis1));
      }
   };

LOGCAT:

06-16 05:47:34.540: E/AndroidRuntime(1373): FATAL EXCEPTION: main
06-16 05:47:34.540: E/AndroidRuntime(1373): Process: com.example.newapplication, PID: 1373
06-16 05:47:34.540: E/AndroidRuntime(1373): java.lang.IllegalMonitorStateException: object not locked by thread before wait()
06-16 05:47:34.540: E/AndroidRuntime(1373):     at java.lang.Object.wait(Native Method)
06-16 05:47:34.540: E/AndroidRuntime(1373):     at java.lang.Object.wait(Object.java:364)
06-16 05:47:34.540: E/AndroidRuntime(1373):     at com.example.newapplication.Tabs.onClick(Tabs.java:100)
06-16 05:47:34.540: E/AndroidRuntime(1373):     at android.view.View.performClick(View.java:4438)
06-16 05:47:34.540: E/AndroidRuntime(1373):     at android.view.View$PerformClick.run(View.java:18422)
06-16 05:47:34.540: E/AndroidRuntime(1373):     at android.os.Handler.handleCallback(Handler.java:733)
06-16 05:47:34.540: E/AndroidRuntime(1373):     at android.os.Handler.dispatchMessage(Handler.java:95)
06-16 05:47:34.540: E/AndroidRuntime(1373):     at android.os.Looper.loop(Looper.java:136)
06-16 05:47:34.540: E/AndroidRuntime(1373):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-16 05:47:34.540: E/AndroidRuntime(1373):     at java.lang.reflect.Method.invokeNative(Native Method)
06-16 05:47:34.540: E/AndroidRuntime(1373):     at java.lang.reflect.Method.invoke(Method.java:515)
06-16 05:47:34.540: E/AndroidRuntime(1373):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-16 05:47:34.540: E/AndroidRuntime(1373):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-16 05:47:34.540: E/AndroidRuntime(1373):     at dalvik.system.NativeStart.main(Native Method)

Solution

  • You can analize the StackTrace of the Exception. It shows the classes, methods and souce line which were called to cause the Exception.

    One cause for the IllegalMonitorStateException is trying to wait on an Object without having synchronized on it. See the Javadoc.

    There are other possible causes and the Exception may be thrown by some library/external code. I think only the StackTrace can help.. this link may also help