문제


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.

도움이 되었습니까?

해결책

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);

다른 팁

Easiest way how to solve it is to create an postDelayed runnable

    private Handler mHandler;
    public static final int DELAY = 5000;

    Runnable mRunnable = new Runnable()
    {

        @Override
        public void run()
        {
            //TODO: do something like mViewPager.setCurrentPage(mIterator);
            mHandler.postDelayed( mRunnable , DELAY );
        }
    };

as You can see it will loop infinitelly. When you want to stop it, just simply call

        mHandler.removeCallbacks( mRunnable );

The TimerTask will runs on it's own thread ( = not the UI thread).

You can simply call setCurrentItem directly on the main thread using a Handler.

For the kotlin extension lover. This can be helpful on viewPage2 auto page change

fun ViewPager2.enableAutoScroll(totalPages: Int): Timer {
    val autoTimerTask = Timer()
    var currentPageIndex = currentItem
    autoTimerTask.schedule(object : TimerTask() {
        override fun run() {
            currentItem = currentPageIndex++
            if (currentPageIndex == totalPages) currentPageIndex = 0
        }
    }, 0, DELAY_FOUR_SECONDS)

    // Stop auto paging when user touch the view
    getRecyclerView().setOnTouchListener { _, event ->
        if (event.action == MotionEvent.ACTION_DOWN) autoTimerTask.cancel()
        false
    }

    return autoTimerTask // Return the reference for cancel
}

fun ViewPager2.getRecyclerView(): RecyclerView {
    val recyclerViewField = ViewPager2::class.java.getDeclaredField("mRecyclerView")
    recyclerViewField.isAccessible = true
    return recyclerViewField.get(this) as RecyclerView
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top