我的项目由于TimerTask错误而不断崩溃

时间:2020-02-04 21:53:22

标签: android android-studio android-fragments android-viewpager

尝试在HomeFragment.java中使用此代码时,我不断收到相同的错误

class SliderTimer extends TimerTask {


        @Override
        public void run() {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (sliderpager.getCurrentItem()<lstSlides.size()-1) {
                                sliderpager.setCurrentItem(sliderpager.getCurrentItem()+1);
                    }
                    else
                        sliderpager.setCurrentItem(0);
                }
            });
        }
    }

这是我收到的运行时错误。

E/AndroidRuntime: FATAL EXCEPTION: Timer-0
    Process: com.example.fypfinaltrial, PID: 30006
    java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.fragment.app.FragmentActivity.runOnUiThread(java.lang.Runnable)' on a null object reference
        at com.example.fypfinaltrial.HomeFragment$SliderTimer.run(HomeFragment.java:117)
        at java.util.TimerThread.mainLoop(Timer.java:562)
        at java.util.TimerThread.run(Timer.java:512)

0 个答案:

没有答案