如何修复 UnsupportedOperationException android

How to fix UnsupportedOperationException android

我正在编写一个 android 应用程序,其中包含 2 分钟的计时器。当计时器完成其 2 分钟警报对话框时将提示警报环,这里我在线程中写了警报环,当我在警报对话框中单击确定按钮时它显示异常 "java.lang.UnsupportedOperationException" 请告诉我如何解决这是我的 activity 代码:

    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(); 
        }

 }

这是我的 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.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)

不要使用 Thread.stop()。请参阅 this 主题以正确关闭话题。

如果您阅读 the documentation for Thread.stop,您会发现它总是抛出 UnsupportedOperationException。它不会停止线程。

无法停止线程;线程必须自行停止。您可以创建一个 volatile boolean 字段,并在单击按钮时将其设置为 true,并在线程检测到该字段为 true.

时停止循环