如何在android中关闭计时器?

时间:2015-02-25 15:12:12

标签: java android timer timertask

您好我的编码我需要创建一个计时器任务来执行一些函数这个函数多次调用所以在运行相同的任务之前我需要cacel旧的运行任务并再次启动这个计时器。我使用了featuredCouponRotationTimer.cancel();但它没有用,我在logcate中遇到了一些错误

public void startPagerRotationTimer(){

    featuredCouponRotationTimer.cancel();
    featuredCouponRotationTimer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            if(!isBeingTouched){
                ActivityMain.getActivityMain().runOnUiThread(new Runnable() {
                    public void run() {

                     Log.i("pagerDurationFeaturedCoupons","setData :working");

                        }
                    }

                });
            }
        }

    }, 0, 2000);
}

Logcat错误

02-25 20:25:18.210      816-816/com.shoplylabs.shoplys E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalStateException: Timer was canceled
            at java.util.Timer.scheduleImpl(Timer.java:561)
            at java.util.Timer.scheduleAtFixedRate(Timer.java:528)
            at com.shoplylabs.shoplys.view.ViewContainerFeaturedCoupon.startPagerRotationTimer(ViewContainerFeaturedCoupon.java:190)
            at com.shoplylabs.shoplys.view.ViewContainerFeaturedCoupon.setData(ViewContainerFeaturedCoupon.java:182)
            at com.shoplylabs.shoplys.adapters.AdapterShoplyCouponStaggeredGrid.getView(AdapterShoplyCouponStaggeredGrid.java:156)
            at com.shoplylabs.shoplys.components.staggeredgridview.StaggeredGridView.obtainView(StaggeredGridView.java:1533)
            at com.shoplylabs.shoplys.components.staggeredgridview.StaggeredGridView.fillDown(StaggeredGridView.java:1245)
            at com.shoplylabs.shoplys.components.staggeredgridview.StaggeredGridView.populate(StaggeredGridView.java:846)
            at com.shoplylabs.shoplys.components.staggeredgridview.StaggeredGridView.onLayout(StaggeredGridView.java:797)
            at android.view.View.layout(View.java:15214)
            at android.view.ViewGroup.layout(ViewGroup.java:4793)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
            at android.view.View.layout(View.java:15214)
            at android.view.ViewGroup.layout(ViewGroup.java:4793)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:15214)
            at android.view.ViewGroup.layout(ViewGroup.java:4793)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:15214)
            at android.view.ViewGroup.layout(ViewGroup.java:4793)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
            at android.view.View.layout(View.java:15214)
            at android.view.ViewGroup.layout(ViewGroup.java:4793)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:15214)
            at android.view.ViewGroup.layout(ViewGroup.java:4793)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:15214)
            at android.view.ViewGroup.layout(ViewGroup.java:4793)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:15214)
            at android.view.ViewGroup.layout(ViewGroup.java:4793)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
            at android.view.View.layout(View.java:15214)
            at android.view.ViewGroup.layout(ViewGroup.java:4793)
            at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:890)
            at android.view.View.layout(View.java:15214)
            at android.view.ViewGroup.layout(ViewGroup.java:4793)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:15214)
            at android.view.ViewGroup.layout(ViewGroup.java:4793)
            at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
            at android.view.View.layout(View.java:15214)
            at android.view.ViewGroup.layout(ViewGroup.java:4793)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:15214)
            at android.view.ViewGroup.layout(ViewGroup.java:4793)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
            at android.view.View.layout(View.java:15214)
            at android.view.ViewGroup.layout(ViewGroup.java:4793)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:15214)
            at android.view.ViewGroup.layout(ViewGroup.java:4793)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2260)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2007)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6364)
            at android.view.Choreographer$CallbackRecord.run(Choreographe

请帮我解决这个问题

2 个答案:

答案 0 :(得分:0)

明确说明原因:

  

java.lang.IllegalStateException:计时器被取消

您需要重新初始化featuredCouponRotationTimer对象,以避免使用IllegalStateException

答案 1 :(得分:0)

不建议使用定时器来使用新代码,您应该查看ScheduledExecutorServices

如果你想保留计时器,请使用:

public void startPagerRotationTimer()
{
    if (featuredCouponRotationTimer != null)
    {
        featuredCouponRotationTimer.cancel();
    }

    featuredCouponRotationTimer = new Timer();
    featuredCouponRotationTimer.scheduleAtFixedRate(
            new TimerTask()
            {
                @Override
                public void run()
                {
                    if (!isBeingTouched)
                    {
                        ActivityMain.getActivityMain().runOnUiThread(
                                new Runnable()
                                {
                                    public void run()
                                    {

                                        Log.i("pagerDurationFeaturedCoupons", "setData :working");
                                    }
                                }
                                                                    );
                    }
                }
            }, 0, 2000);
}