java.lang.IllegalStateException: No activity

前端 未结 5 909
自闭症患者
自闭症患者 2020-12-09 19:53

I\'m building an Android app for which I\'d like my first activity to be composed of 2 tabs, one for the user\'s profile, and one for the friends\' activity. For these tabs,

相关标签:
5条回答
  • 2020-12-09 20:36

    I got this error too. Finally, found out that I was overriding the methods onResume() onStop() incorrectly

    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }
    @Override
    protected void onStop() {
        super.onResume();
        finish();
    }
    

    Changed it to

     @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }
    @Override
    protected void onStop() {
        super.onStop();
        finish();
    }
    

    So Silly Mistake :P

    0 讨论(0)
  • 2020-12-09 20:37

    Finally found out what this was about: the "no activity" crash was due to the fact that I was using a wrong FragmentManager to nest Fragments.

    For nested fragments, the only valid FragmentManager is the one obtained in the containing Fragment, by calling getChildFragmentManager().

    0 讨论(0)
  • 2020-12-09 20:37

    I was creating a fragment in myActivity.onCreate() by calling:

    getSupportFragmentManager()
        .beginTransaction()
        .remove(frag)
        .replace(idOfViewBeingReplaced, frag)
        .commit();
    

    While viewing my app I would hit the power-off button and then the app would crash. Apparently, when the device is powered off, the Activity and Fragment states are saved, detached, and then onCreate() and onCreateView() are called on the Fragments. In this state, .remove(frag) cannot be called or else it will throw:

    illegal state: no activity

    So, I just deleted .remove(frag) and everything is working again.

    0 讨论(0)
  • 2020-12-09 20:39

    In my case I using a splash screen and I have had MainLauncher = true in the MainActivity.

    0 讨论(0)
  • 2020-12-09 20:45

    You need that the transaction to be committed after an activity's state is saved...

    Use ft.commitAllowingStateLoss() instead of ft.commit()

    That will solve your problem.

    Source

    0 讨论(0)
提交回复
热议问题