为什么我的处理程序不执行?

时间:2014-12-26 12:00:09

标签: java android

我有以下代码:

   int x=0; 
    private void startTimerThread() {
        System.out.println("enter");
        System.out.println("percentage"+percentage); 
        System.out.println("x"+x); 

        final Handler handler = new Handler();
        Runnable runnable = new Runnable() {
            public void run() {
                for (x = 0; x>= percentage; x++ ) {  
                    try {
                        Thread.sleep(1000);
                    }    
                    catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    handler.post(new Runnable(){
                        public void run() {
                            textpercentage.animate(x, x++);
                            System.out.println("enter"+x);
                    }
                });
                }
            }
        };
        new Thread(runnable).start();
    }

我正在尝试使用及时的文本视图为textview上的数字设置动画,但是当我从onCreate之外的代码调用startTimerThread时,我也没有显示文本视图而不执行system.out。我在这里想念什么?

1 个答案:

答案 0 :(得分:1)

试试这个

Handler handler = new Handler();
    int delay=1000;
    Runnable rann=new Runnable() {
        @Override
        public void run() {
            //Write Your logic here which you want to perform periodically
            System.out.println("Handler is running : ");
            //to call the same thread repeatedly calling handler again
            handler.postDelayed(rann, delay);
        }
    };


    private void startHandler() {
        //here the handler will executes the rannable after that particulary delay milli seconds
        handler.postDelayed(rann, delay);
    }

    private void stopHandler() {
        handler.removeCallbacks(rann);
    }