0

I am writing an android application that consists of timer with 2 minutes. When timer finishes its 2 minutes alert dialog will prompts with alert ring, Here i wrote the alert ring in a thread when i click ok button in alert dialog it is showing an exception "java.lang.UnsupportedOperationException" please tell me how to solve it this is my activity code:

    public class CounterClass extends CountDownTimer 
    {

        public CounterClass(long millisInFuture, long countDownInterval)
        {
            super(millisInFuture, countDownInterval);

        }

        @SuppressLint("NewApi")
        @TargetApi(Build.VERSION_CODES.GINGERBREAD)
        @Override
        public void onTick(long millisUntilFinished) 
        {
            // TODO Auto-generated method stub

            long millis = millisUntilFinished;
            String hms = String.format("%02d:%02d", TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
                    TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
            System.out.println(hms);
            timer_text.setText(hms);
        }

        @Override
        public void onFinish()
        {
            // TODO Auto-generated method stub
            Test_Status=false;
            Time_Completed = true;;
            timer_alert_dialog = new AlertDialog.Builder(LifeTest.this);
            timer_alert_dialog.setIcon(R.drawable.ok);
            timer_alert_dialog.setTitle("2C1 PANEL");
            timer_alert_dialog.setMessage("Test Got Completed");
            timer_alert_dialog.setCancelable(false);


            timer_alert_dialog.setPositiveButton("OK", new DialogInterface.OnClickListener()
            {

                @Override
                public void onClick(DialogInterface dialog, int which)
                {

                    Alert_Thread.stop(); 

                }
            });

            AlertDialog Dialog_Center_Finished = timer_alert_dialog.show();
            TextView message_center_finished = (TextView)Dialog_Center_Finished.findViewById(android.R.id.message);
            message_center_finished.setGravity(Gravity.CENTER);
            Alert_Ring = MediaPlayer.create(LifeTest.this,R.raw.completed);
            Alert_Thread = new Thread(new Runnable()
            {

                @Override
                public void run() 
                {

                    while(true)
                    {
                        Alert_Ring.start();
                    }

                }
            });

            Alert_Thread.start(); 
        }

 }

This is my Logcat:

03-10 12:22:47.024: E/AndroidRuntime(4637): FATAL EXCEPTION: main
03-10 12:22:47.024: E/AndroidRuntime(4637): Process: com.example.testpanel2c1, PID: 4637
03-10 12:22:47.024: E/AndroidRuntime(4637): java.lang.UnsupportedOperationException
03-10 12:22:47.024: E/AndroidRuntime(4637):     at java.lang.Thread.stop(Thread.java:1052)
03-10 12:22:47.024: E/AndroidRuntime(4637):     at java.lang.Thread.stop(Thread.java:1042)
03-10 12:22:47.024: E/AndroidRuntime(4637):     at com.example.testpanel2c1.LifeTest$CounterClass$1.onClick(LifeTest.java:370)
03-10 12:22:47.024: E/AndroidRuntime(4637):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
03-10 12:22:47.024: E/AndroidRuntime(4637):     at android.os.Handler.dispatchMessage(Handler.java:110)
03-10 12:22:47.024: E/AndroidRuntime(4637):     at android.os.Looper.loop(Looper.java:193)
03-10 12:22:47.024: E/AndroidRuntime(4637):     at android.app.ActivityThread.main(ActivityThread.java:5292)
03-10 12:22:47.024: E/AndroidRuntime(4637):     at java.lang.reflect.Method.invokeNative(Native Method)
03-10 12:22:47.024: E/AndroidRuntime(4637):     at java.lang.reflect.Method.invoke(Method.java:515)
03-10 12:22:47.024: E/AndroidRuntime(4637):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
03-10 12:22:47.024: E/AndroidRuntime(4637):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
03-10 12:22:47.024: E/AndroidRuntime(4637):     at dalvik.system.NativeStart.main(Native Method)
RD Division Medequip
  • 1,605
  • 4
  • 18
  • 32
  • 1
    And ideally as short but complete code as possible - we don't need to see the commented out part, if it's not relevant to the question. – Jon Skeet Mar 10 '15 at 06:50
  • What do you think `Thread.stop` does, and what does the documentation say it does? – user253751 Mar 10 '15 at 06:55
  • I posted my logcat above @Jens – RD Division Medequip Mar 10 '15 at 06:55
  • @RDDivisionMedequip Please have a look on the [Java Coding Conventions](http://www.oracle.com/technetwork/java/codeconventions-135099.html). You should use [camelCase](https://en.wikipedia.org/wiki/CamelCase) (start with lower case) for you variables. – Alexander_Winter Mar 10 '15 at 08:39

2 Answers2

1

Do not use Thread.stop(). See this topic to properly close a thread.

Community
  • 1
  • 1
ThomasThiebaud
  • 11,331
  • 6
  • 54
  • 77
1

If you read the documentation for Thread.stop you would see that it always throws UnsupportedOperationException. It does not stop the thread.

There is no way to stop a thread; threads must stop themselves. You might create a volatile boolean field, and set it to true when the button is clicked, and make the thread stop looping when it detects the field is true.

user253751
  • 57,427
  • 7
  • 48
  • 90