[android]非法状态异常对 getChildFragmentManager() 呼吁后台。

发布时间: 2016/9/28 1:05:58
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有以下代码︰

RelativeLayout howItWorks;
RelativeLayout howWeHelp;
RelativeLayout knowYourBloodPressure;
RelativeLayout learnAboutBirthControl;
LinearLayout disposableLayout;
LinearLayout fragmentContainer;
EducationalFragment educationalFragment;
ReactiveButton button;


@Override
public void onAttach(Context context) {
    super.onAttach(context);

}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.fragment_educational, container, false);
    //View view = inflater.inflate(R.layout.fragment_ocp_get_prescription, container, false);
    if (view != null)
    {
        ButterKnife.inject(this, view);
        //Artisan.link(linkText, "www.plannedparenthood.org/health-center", "https://www.plannedparenthood.org/health-center");
    }
    howItWorks = (RelativeLayout) view.findViewById(R.id.HowItWorks);
    howWeHelp = (RelativeLayout) view.findViewById(R.id.HowWeHelp);
    knowYourBloodPressure = (RelativeLayout) view.findViewById(R.id.KnowYourBloodPressure);
    learnAboutBirthControl = (RelativeLayout) view.findViewById(R.id.LearnAboutBirthControl);
    disposableLayout =(LinearLayout)view.findViewById(R.id.disposableView);
    fragmentContainer = (LinearLayout)view.findViewById(R.id.fragmentContainer);
    button = (ReactiveButton)view.findViewById(R.id.action_continue);
    return view;
}


@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);


    howItWorks.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            educationalFragment = new EducationalFragment();
            disposableLayout.setVisibility(View.GONE);
            button.setVisibility(View.INVISIBLE);
            getChildFragmentManager().beginTransaction().replace(R.id.fragmentContainer,educationalFragment,"eduFrag").addToBackStack("howItWorks").commit();
            educationalFragment.setFirstScreen(1);

        }
    });
    howWeHelp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            disposableLayout.setVisibility(View.GONE);
            educationalFragment = new EducationalFragment();
            button.setVisibility(View.INVISIBLE);
            getChildFragmentManager().beginTransaction().replace(R.id.fragmentContainer,educationalFragment,"eduFrag").addToBackStack("howWeHelp").commit();
            educationalFragment.setFirstScreen(2);
        }
    });
    knowYourBloodPressure.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            educationalFragment = new EducationalFragment();
            disposableLayout.setVisibility(View.GONE);
            button.setVisibility(View.INVISIBLE);
            getChildFragmentManager().beginTransaction().replace(R.id.fragmentContainer,educationalFragment,"eduFrag").addToBackStack("knowYourBloodPressure").commit();
            educationalFragment.setFirstScreen(3);
        }
    });
    learnAboutBirthControl.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            educationalFragment = new EducationalFragment();
            disposableLayout.setVisibility(View.GONE);
            button.setVisibility(View.INVISIBLE);
            getChildFragmentManager().beginTransaction().replace(R.id.fragmentContainer,educationalFragment,"eduFrag").addToBackStack("learnAboutBirthControl").commit();
            educationalFragment.setFirstScreen(4);
        }
    });
}

@Override
public void onResume() {
    super.onResume();
    if(educationalFragment!=null) {
        getChildFragmentManager().beginTransaction().remove(educationalFragment).commit();
    }
}

@OnClick(R.id.action_continue)
public void onContinue()
{
    if (listener != null)
    {
        listener.onContinueClick();
    }
}

public void popBackStackMethod(){


    this.getChildFragmentManager().popBackStack();
    disposableLayout.setVisibility(View.VISIBLE);



}

我得到非法状态异常时,调用 popBackStack :

 FATAL EXCEPTION: main
 Process: com.ppfa.stdtestca.release, PID: 8846
 java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
     at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1493)
     at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1511)
     at android.support.v4.app.FragmentManagerImpl.popBackStack(FragmentManager.java:575)
     at com.ppfa.stdtestca.categories.ui.ocp.OCPGettingPrescriptionFragment.popBackStackMethod(OCPGettingPrescriptionFragment.java:135)
     at com.ppfa.stdtestca.core.ui.MainActivity.onBackPressed(MainActivity.java:164)
     at android.app.Activity.onKeyUp(Activity.java:2725)
     at android.view.KeyEvent.dispatch(KeyEvent.java:2699)
     at android.app.Activity.dispatchKeyEvent(Activity.java:3026)
     at com.bluerock.sapphire.util.CompositeWindowCallback.dispatchKeyEvent(CompositeWindowCallback.java:49)
     at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:317)
     at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4327)
     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4298)
     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3995)
     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876)
     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4052)
     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876)
     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4028)
     at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4189)
     at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2365)
     at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1961)
     at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1952)
     at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2342)
     at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
     at android.os.MessageQueue.nativePollOnce(Native Method)
     at android.os.MessageQueue.next(MessageQueue.java:323)
     at android.os.Looper.loop(Looper.java:136)
     at android.app.ActivityThread.main(ActivityThread.java:6077)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

有人能帮我明白发生了什么事?

解决方法 1:

检查是否这有助于︰

  public void popBackStackMethod(){
               if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
                    this.getChildFragmentManager().popBackStack();
                    disposableLayout.setVisibility(View.VISIBLE);
                } 
    }
官方微信
官方QQ群
31647020