简体   繁体   English

Kotlin:切换片段时尚未初始化lateinit属性userDatabase

[英]Kotlin: lateinit property userDatabase has not been initialized when switching fragments

I have an activity with Three Tabs and Four Fragments.我有一个包含三个标签和四个片段的活动。 On one of the tabs (Profile tab), I have a fragment (ProfileFragment) that has a button to redirect the user to another fragment (EditorFragment) to edit the user's profile.在其中一个选项卡(配置文件选项卡)上,我有一个片段 (ProfileFragment),它有一个按钮可以将用户重定向到另一个片段 (EditorFragment) 以编辑用户的配置文件。

class ProfileFragment : Fragment() {
   
 ...

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val v = inflater.inflate(R.layout.fragment_profile, container, false)

        val btn = v.findViewById<View>(R.id.editProfileButton) as Button

        btn.setOnClickListener {
            val fragment = EditorFragment()
            val fragmentManager = activity!!.supportFragmentManager
            val transaction = fragmentManager.beginTransaction()
            transaction.replace(R.id.fragmentContainer, fragment)

           transaction.addToBackStack(null)
            transaction.commit()
        }
        return v
    }

The EditorFragment also belongs to the Profile Tab: EditorFragment 也属于 Profile 选项卡:

        class EditorFragment : Fragment() {
        
            private lateinit var userDatabase: DatabaseReference
            private var callback: AppCallback? = null
    
        fun setCallback(callback: AppCallback) {
            this.callback = callback
            userId = callback.onGetUserId()
            userDatabase = callback.getUserDatabase().child(userId)
        }


        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)

            populateInfo(view)
        }

    
       fun populateInfo(view: View) {
        userDatabase.addListenerForSingleValueEvent(object : ValueEventListener {
            override fun onDataChange(p0: DataSnapshot) {
             ...
            }

When I give priority to the EditorFragment to open first, everything goes well but when I am in ProfileFragment and try to use the button to redirect to Editor Fragment, the app crashes.当我优先打开 EditorFragment 时,一切顺利,但是当我在 ProfileFragment 中并尝试使用按钮重定向到 Editor Fragment 时,应用程序崩溃。

ERROR:错误:

lateinit property userDatabase has not been initialized
at com.example.app.fragments.EditorFragment.populateInfo(EditorFragment.kt:138)

Which refers to this line on EditorFragment populateInfo():它指的是 EditorFragment populateInfo() 上的这一行:

userDatabase.addListenerForSingleValueEvent(object : ValueEventListener {

Aditional Information that may help:可能有帮助的其他信息:

  • The button is functioning when I delete everything related to the issue.当我删除与该问题相关的所有内容时,该按钮正在运行。

Main Activity:主要活动:

class MainActivity : AppCompatActivity(), MainCallback {
             
    private lateinit var userDatabase: DatabaseReference
    private var profileFragment: ProfileFragment? = null
    private var editorFragment: EditorFragment? = null

    ...

        navigationTabs.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
        ...

            override fun onTabSelected(tab: TabLayout.Tab?) {
                when(tab) {
                    profileTab -> {
                        if (profileFragment == null) {
                            profileFragment = ProfileFragment()
                            profileFragment!!.setCallback(this@MainActivity)
                        }
                        replaceFragment(profileFragment!!)

                        if (editorFragment == null) {
                            editorFragment = EditorFragment()
                            editorFragment!!.setCallback(this@MainActivity)
                        }
                        replaceFragment (editorFragment!!)
                    }
         ...


     fun replaceFragment(fragment: Fragment){
         supportFragmentManager.beginTransaction()
        .replace(R.id.fragmentContainer, fragment)
        .commit()
     }

When you give priority to EditorFragment to open first your onTabSelectListener in triggered hence calling your setCallBack that initializes your database, when you open the ProfileFragment first it is not triggering your onTabSelectedListener hence it doesnt call your setCallBack and it doesn't initializes your database.当您优先让 EditorFragment 首先打开您的 onTabSelectListener 时,会调用初始化数据库的setCallBack ,当您首先打开ProfileFragment 时,它不会触发您的 onTabSelectedListener,因此它不会调用您的setCallBack并且不会初始化您的数据库。 Let me know if it helps you to solve the problem.让我知道它是否可以帮助您解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM