0

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

    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$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)
4

2 回答 2

1

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

于 2015-03-10T07:00:56.183 回答
1

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

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

于 2015-03-10T07:03:35.513 回答