当我停止按下按钮时,应用程序崩溃

时间:2016-10-18 20:32:55

标签: android

只要按住按钮,此代码就会旋转图像。当我停止按下按钮时问题就开始了 - 应用程序崩溃了。你能帮我发现问题吗?

 Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable(){
                @Override
                public void run(){
                        if (btn.isPressed())
                        {
                            i++;
                            eagleview = (ImageView) findViewById(imageView3);

                            Matrix matrix = new Matrix();
                            eagleview.setScaleType(ImageView.ScaleType.MATRIX);
                            matrix.postRotate((float) i, eagleview.getHeight()/2,eagleview.getWidth()/2);
                            eagleview.setImageMatrix(matrix);


                        }
                }
            });

        }
    }, 0, 10);

}}

1 个答案:

答案 0 :(得分:0)

 Timer timer = new Timer();

 OnButtonClick(){
 //Check for timer is running or not according to it cancel previous scheduled task

  timer.cancel();  //Terminates this timer,discarding any currently scheduled tasks.

  timer.purge();   // Removes all cancelled tasks from this timer's task queue.
  timer.schedule(new TimerTask() {
    @Override
    public void run() {
        runOnUiThread(new Runnable(){
            @Override
            public void run(){



                    if (btn.isPressed())
                    {
                        i++;
                        eagleview = (ImageView) findViewById(imageView3);

                        Matrix matrix = new Matrix();
                        eagleview.setScaleType(ImageView.ScaleType.MATRIX);
                        matrix.postRotate((float) i, eagleview.getHeight()/2,eagleview.getWidth()/2);
                        eagleview.setImageMatrix(matrix);


                    }



            }
        });

    }
}, 0, 10);

}} }