简体   繁体   English

在 onCreateView 中初始化的变量出现“Lateinit 属性尚未初始化”错误

[英]“Lateinit property has not been initialized” error on a variable initialized in onCreateView

The app I'm developing has three classes.我正在开发的应用程序有三个类。 MainActivity (not important here), MyFragment and MyClass . MainActivity (这里不重要), MyFragmentMyClass I have a lateinit List inside MyFragment - it is lateinit because it will be used by some methods.我在MyFragment中有一个lateinit列表 - 它是lateinit因为它将被某些方法使用。 (I'm using kotlin synthetics by the way) (顺便说一句,我正在使用 kotlin 合成物)

class MyFragment: Fragment()
    lateinit var myViewsList: List<View>
    lateinit var viewA: View
    lateinit var viewB: View
    lateinit var viewC: View

    override fun onCreateView(
    //inflating the layout...
    ): View? {
        val view = inflater.inflate(R.layout.fragment_timer, container, false)

        val button: Button = view.myButton
        viewA = view.myViewA
        viewB = view.myViewB
        viewC = view.myViewC

        myViewsList =
            listOf<View>(viewA,viewB,viewC) 

        myButton.setOnClickListener() {
            MyClass().myMethod()
        }

        return view
    }

And then MyClass :然后MyClass

class MyClass() {
    fun showViews {
        MyFragment().myViewsList.forEach { // this line is causing the error
            it.visibility = View.VISIBLE
        }
    }

    fun myMethod() {
        //do some stuff
        //delay...
        showViews()
    }


}

But I get the "lateinit property myViewsList not initialized" error whenever showViews() is called (and it isn't called when the fragment is destroyed, started or something like that. In my app, it'd take you ~10 seconds to call it - I've just put it in a OnClickListener for simplicity - and there aren't any activity changes during those 10 seconds).但是,每当showViews()时,我都会收到“lateinit property myViewsList not initialized”错误(并且在片段被销毁、启动或类似的情况下不会调用它。在我的应用程序中,大约需要 10 秒调用它——为简单起见,我只是将它放在OnClickListener中——在这 10 秒内没有任何活动变化)。

I don't understand why, as it is initialized in OnCreateView myViewsList = listOf<View>(viewA,viewB,viewC) .我不明白为什么,因为它在 OnCreateView myViewsList = listOf<View>(viewA,viewB,viewC)中初始化的。 Any calls involving myViewsList inside MyFragment work just fine, but not inside MyClass .任何涉及myViewsList内的MyFragment的调用都可以正常工作,但不能在MyClass内工作。 So, why is this happening?那么,为什么会这样呢? and what would be the correct way of calling or initializing it?调用或初始化它的正确方法是什么?

I may be wrong here, but I dont think an Android fragment class will work as any other regular class.我在这里可能错了,但我认为 Android 片段 class 不会像任何其他常规 class 一样工作。 When you are invoking MyFragment().myViewsList you are not calling the same instance of your fragment and as you are calling this variable "myViewsList" that hasnt been initialized, it crashes.当您调用MyFragment().myViewsList时,您不会调用片段的同一实例,并且当您调用尚未初始化的此变量“myViewsList”时,它会崩溃。

To make it work, you will have to pass your list and the view that would you like to make it visible.为了让它工作,你必须传递你的列表和你想让它可见的视图。

class MyFragment: Fragment()
    lateinit var myViewsList: List<View>
    lateinit var viewA: View
    lateinit var viewB: View
    lateinit var viewC: View

    override fun onCreateView(
    //inflating the layout...
    ): View? {
        val view = inflater.inflate(R.layout.fragment_timer, container, false)

        val button: Button = view.myButton
        viewA = view.myViewA
        viewB = view.myViewB
        viewC = view.myViewC

        myViewsList =
            listOf<View>(viewA,viewB,viewC) 

        myButton.setOnClickListener() {
            MyClass(myViewsList).myMethod()
        }

        return view
    }

and your class should be like this:你的 class 应该是这样的:

class MyClass(private val views: List<View>) {
    fun showViews {
        views.forEach { // this line is causing the error
            it.visibility = View.VISIBLE
        }
    }

    fun myMethod() {
        //do some stuff
        //delay...
        showViews()
    }


}

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

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