简体   繁体   English

如何在 android 片段中使用辅助构造函数分配后期初始化变量?

[英]How to assign late initialize variable with secondary constructor in android fragment?

I am using Kotlin and fragments in my app.我在我的应用程序中使用 Kotlin 和片段。 I created a secondary constructor to keep primary constructor empty in order to avoid the error of could not find fragment constructor.我创建了一个辅助构造函数来保持主构造函数为空,以避免找不到片段构造函数的错误。 I assigned late initialize variable in secondary constructor of fragment but it is null when called, causing null pointer exception.我在片段的辅助构造函数中分配了后期初始化变量,但在调用时它是 null,导致 null 指针异常。 Here is the code sequence:这是代码序列:

class VideoFragment(
) : Fragment(),
VideoFileListener {

private lateinit var mainContext: Context
private lateinit var itemClickListener: ItemClickListener

constructor(
    mainContext: Context,
    itemClickListener: ItemClickListener
) : this() {
    this.mainContext = mainContext
    this.itemClickListener = itemClickListener
}
}

companion object {
    @JvmStatic
    fun newInstance(
        mainContext: Context,
        itemClickListener: ItemClickListener
    ) =
        VideoFragment(mainContext, itemClickListener)
}

I access my fragment with static newInstance function.我使用 static newInstance function 访问我的片段。 Why these variables are not initialized in secondary constructor?为什么这些变量没有在辅助构造函数中初始化?

You kind of miss the point of the newInstance static method here.您有点错过这里的newInstance static 方法的要点。 The whole point of that method is to put the given parameters supplied there into the fragment arguments, so the fragment can later retrieve those parameters from it's arguments.该方法的重点是将那里提供的给定参数放入片段 arguments 中,以便片段稍后可以从它的 arguments 中检索这些参数。 This is necessary because on configuration change your Fragment will be recreated using the no-arg constructor, and then you'll have someplace to restore the original arguments for your Fragment.这是必要的,因为在更改配置时,您的 Fragment 将使用无参数构造函数重新创建,然后您将有地方为您的 Fragment 恢复原始 arguments。 Have a look at this answer for further explanation and example: https://stackoverflow.com/a/9245510/5601663看看这个答案以获得进一步的解释和例子: https://stackoverflow.com/a/9245510/5601663

This has another effect.这还有另一个效果。 You can't really have callbacks as Fragment arguments, because these arguments need to be Serializable or Parcelable .您不能真正将回调作为片段 arguments,因为这些 arguments 需要是SerializableParcelable I highly recommend you watch this video from the point I linked, it elaborates further on this: https://youtu.be/dcYKW48tHQ4?t=1867我强烈建议您从我链接的地方观看此视频,它对此进行了进一步详细说明: https://youtu.be/dcYKW48tHQ4?t=1867

One last thing, if you got NullPointerException then it's cause by something other then your lateinit property not being initialized, because that would produce an error similar to this: kotlin.UninitializedPropertyAccessException: lateinit property foo has not been initialized .最后一件事,如果你得到NullPointerException ,那么它的原因是你的 lateinit 属性没有被初始化,因为这会产生类似于这样的错误: kotlin.UninitializedPropertyAccessException: lateinit property foo has not been initialized

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

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