在Runnable中执行Fragment时出错

时间:2018-04-23 20:37:10

标签: java android android-fragments handler runnable

我有一个活动和一个登录按钮。当我单击登录按钮时,进度条将加载10秒,之后片段将加载。但是我收到了一个错误。

这是我的代码:

handler=new Handler();
    runnable=new Runnable() {
        @Override
        public void run() {
            if (loader_fragment != null) {
                AVLoadingIndicatorView loader = loader_fragment.getView().findViewById(R.id.loder_login);
                loader.setVisibility(View.INVISIBLE);
                timer.cancel();
            }
            Home_Screen home_screen = (Home_Screen) getFragmentManager()
                .findFragmentById(R.id.Home_screen_fragment);

            fragmentManager1 = getFragmentManager();
            fragmentTransaction1 = fragmentManager1.beginTransaction();
            fragmentTransaction1.setCustomAnimations(R.animator
                .slide_in_left, R.animator.slide_out_left, R.animator
                .slide_in_right, R.animator.slide_out_right);
            //Home_screen_student home_screen_student = new
            //Home_screen_student();
            fragmentTransaction1.replace(R.id.Home_screen_fragment,
            new Home_Screen());
            fragmentTransaction1.addToBackStack(null);
            fragmentTransaction1.commit();
        }
    };

    timer=new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            handler.post(runnable);
        }
    }, 10000,1000);
}

logcat的:

04-24 02:01:07.437 12567-12567/? E/Zygote: no v2
04-24 02:03:09.912 12567-12567/com.example.user.attendance E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.user.attendance, PID: 12567
    java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
        at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1433)
        at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1451)
        at android.app.BackStackRecord.commitInternal(BackStackRecord.java:687)
        at android.app.BackStackRecord.commit(BackStackRecord.java:663)
        at com.example.user.attendance.MainActivity$3$2.run(MainActivity.java:186)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:7402)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

片段像循环一样从左到右打开,直到app停止。

0 个答案:

没有答案