java.lang.IllegalStateException: FragmentManager 尚未附加到主机

java.lang.IllegalStateException: FragmentManager has not been attached to a host

我正在使用 MVP 并创建了一个回调,该回调在登录屏幕中按下按钮时执行,并且实现在 MainActivity 中,执​​行回调片段后替换(不工作) , 我也检查过了。

if(!isAdded){
   return null
}

我已经尝试为每个片段创建新实例,但仍然没有成功。我被困了一段时间了,请帮帮我。

主要Activity

 class MainActivity : AppCompatActivity(), IMainActivityContract.View {
    private lateinit var fragmentManager: FragmentTransaction

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        Constants.mAuth = FirebaseAuth.getInstance()
        changeFragment(Constants.NAV_FRAG_LOGIN)
    }


    override fun changeFragment(fragmentID: Int) {
        if(supportFragmentManager.isDestroyed || supportFragmentManager.fragments.isEmpty()){
            fragmentManager = supportFragmentManager.beginTransaction()
        }

        try {
            when (fragmentID) {
                Constants.NAV_FRAG_LOGIN -> {
                    fragmentManager.replace(R.id.fragmentContainer,FragmentLogin()).commit()
                }

                Constants.NAV_FRAG_MAIN -> {
                    fragmentManager.replace(R.id.fragmentContainer, FragmentMain()).commit()

                }

                Constants.NAV_FRAG_ADDITION -> {
                    fragmentManager.replace(R.id.fragmentContainer,FragmentAddition()).commit()

                }

                Constants.NAV_FRAG_CONFIGURATION -> {
                    fragmentManager.replace(R.id.fragmentContainer,FragmentConfiguration()).commit()

                }
            }
        } catch (message: IllegalStateException) {
            Log.d("LoginDebug", "Exception is: $message")
            message.printStackTrace()
        }
    }
}

主要Activity合同

    interface IMainActivityContract {
       interface View{
           fun changeFragment(fragmentID: Int)
       }

       interface Presenter{
          //not yet implemented
       }
} 

触发回调的片段

class FragmentLogin : Fragment(), IFragmentLoginContract.View {
        private var fragmentLoginBinding: FragmentLoginBinding? = null
        private lateinit var fragmentLoginPresenter: IFragmentLoginContract.Presenter
        private lateinit var mainActivityView: IMainActivityContract.View

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
             val view = inflater.inflate(R.layout.fragment_login, container, false)

             mainActivityView = MainActivity()
             fragmentLoginPresenter = FragmentLoginPresenter(this)
             fragmentLoginBinding = FragmentLoginBinding.bind(view)
             
              onGmailLogin()
              return view
        }

 private fun onGmailLogin() {
        fragmentLoginBinding?.frameLayoutGmailButtonLogin!!.setOnClickListener {
            fragmentLoginPresenter.onGoogleLogin()

            mainActivityView.changeFragment(Constants.NAV_FRAG_MAIN)
        }
    }
}

错误日志

Exception is: java.lang.IllegalStateException: FragmentManager has not been attached to a host. 2020-08-27 20:19:05.637 8456-8456/me.timezoneplanner W/System.err: java.lang.IllegalStateException: FragmentManager has not been attached to a host. 2020-08-27 20:19:05.637 8456-8456/me.timezoneplanner W/System.err: at androidx.fragment.app.FragmentManager.enqueueAction(FragmentManager.java:1739) 2020-08-27 20:19:05.637 8456-8456/me.timezoneplanner W/System.err: at androidx.fragment.app.BackStackRecord.commitInternal(BackStackRecord.java:321) 2020-08-27 20:19:05.637 8456-8456/me.timezoneplanner W/System.err: at androidx.fragment.app.BackStackRecord.commit(BackStackRecord.java:286) 2020-08-27 20:19:05.638 8456-8456/me.timezoneplanner W/System.err: at me.timezoneplanner.activities.MainActivity.changeFragment(MainActivity.kt:41) 2020-08-27 20:19:05.638 8456-8456/me.timezoneplanner W/System.err: at me.timezoneplanner.fragments.login.FragmentLogin$onGmailLogin.onClick(FragmentLogin.kt:53)

您不应使用 MainActivity() 的新实例初始化 mainActivityView,而应使用其正确的父实例初始化它。

替换

mainActivityView = MainActivity()

mainActivityView = (activity as MainActivity)