Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
457 views
in Technique[技术] by (71.8m points)

timer - Android ViewPager automatically change page


I want to schedule an action to change automatically my ViewPager pages. I've tried:

@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...

    swipeTimer = new Timer();
    swipeTimer.schedule(new TimerTask() {

         @Override
         public void run() {
            if (currentPage == NUM_PAGES) {
                currentPage = 0;
            }
            featureViewPager.setCurrentItem(currentPage++, true);
         }
    }, 100, 500);

but I'm always getting:

E/AndroidRuntime(5381): FATAL EXCEPTION: Timer-0
E/AndroidRuntime(5381): java.lang.IllegalStateException: Must be called from main thread of process

I'm already in main thread right? How can I solve this?
Thanks for your time.

EDIT:
====================================


Thanks for all your answers. Based on these responses I came across 2 solutions:
Solution 1:

swipeTimer = new Timer();
            swipeTimer.schedule(new TimerTask() {

                @Override
                public void run() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (currentPage == NUM_PAGES) {
                                currentPage = 0;
                            }
                            featureViewPager.setCurrentItem(currentPage++, true);
                        }
                    });
                }
            }, 500, 3000);

Solution 2:

final Handler handler = new Handler();
                final Runnable Update = new Runnable() {
                    public void run() {
                        if (currentPage == NUM_PAGES) {
                            currentPage = 0;
                        }
                        featureViewPager.setCurrentItem(currentPage++, true);
                    }
                };

            swipeTimer = new Timer();
            swipeTimer.schedule(new TimerTask() {

                @Override
                public void run() {
                    handler.post(Update);
                }
            }, 500, 3000);

Which one is better or they are the same?
Thanks once again.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want to use thread in the main UI then you need to use a hander to hand it.

Handler handler = new Handler();

        Runnable update = new Runnable()  {

            public void run() {
                if ( currentPage == NUM_PAGES ) {

                    currentPage = 0;
                }
                featureViewPager.setCurrentItem(currentPage++, true);
            }
        };


        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {
                handler.post(update);
            }
        }, 100, 500);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...