Search code examples
javaandroidunsupportedoperation

How to fix UnsupportedOperationException android


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)

Solution

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