Fatal Exception: java.lang.IllegalStateException No activity

余生颓废 提交于 2019-12-24 12:19:06

问题


The Stacktrace I got in crashlytics fabric is as follows,

Fatal Exception: java.lang.IllegalStateException: No activity
   at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1058)
   at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1053)
   at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1862)
   at android.app.Fragment.performActivityCreated(Fragment.java:1724)
   at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:915)
   at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1071)
   at android.app.BackStackRecord.run(BackStackRecord.java:684)
   at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1456)
   at android.app.FragmentManagerImpl$1.run(FragmentManager.java:444)
   at android.os.Handler.handleCallback(Handler.java:733)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:136)
   at android.app.ActivityThread.main(ActivityThread.java:5398)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:680)
   at dalvik.system.NativeStart.main(NativeStart.java)
Show all 74 Threads

I am not finding it helpful in any way. Can anybody tell me how can I trace it and how to find which activity is causing problem and what may be the problem?


回答1:


You are using wrong FragmentManager to nest the fragments. You should use fragmentManager instance returned by

getChildFragmentManager();  

instead of using

getSupportFragmentManager();

You can get more information about nested fragments here : https://developer.android.com/about/versions/android-4.2.html#NestedFragments

You can embed fragments inside fragments. This is useful for a variety of situations in which you want to place dynamic and re-usable UI components into a UI component that is itself dynamic and re-usable. You can insert fragments into each fragment page.

To nest a fragment, simply call getChildFragmentManager() on the Fragment in which you want to add a fragment. This returns a FragmentManager that you can use like you normally do from the top-level activity to create fragment transactions. For example, here’s some code that adds a fragment from within an existing Fragment class:

Fragment videoFragment = new VideoPlayerFragment();
FragmentTransaction transaction = 
      getChildFragmentManager().beginTransaction();
    transaction.add(R.id.video_fragment, videoFragment).commit();

From within a nested fragment, you can get a reference to the parent fragment by calling getParentFragment().



来源:https://stackoverflow.com/questions/46157134/fatal-exception-java-lang-illegalstateexception-no-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!