Kotlin 伴随对象在被引用时为 null

Kotlin Companion Object is null when referenced

不确定我是否正确使用了这个伴随对象。

数据class:

data class AppModel(
    val name: String,
    val items: List<ItemModel>,
)

设置数据:

open class PathManager() {

    companion object {
        var shared = PathManager()    // SHARED OBJECT IN QUESTION
    }

    var app: AppModel? = null

    fun setUp() {
        // Show loading...

        // Parse the JSON
        val gson = GsonBuilder().create()
        val text = mainActivity.resources.openRawResource(R.raw.jsonFile)  
            .bufferedReader().use { it.readText() }
        val appModel = gson.fromJson(text, AppModel::class.java)

        app = appModel

        performInitialPath()
    } 

    private fun performInitialPath() {

        val app = app?.let { it } ?: error("Attempted to start without an App Config.")

        this.app = app    // DEBUGGER SHOWS CORRECT VALUES HERE

        // Removed irrelevant code for readability, builds 'initialFoo' here

        this.mainActivity.addFragment(initialFoo)
    }
}

Fragment Builder Class(在此处查找路径管理器伴随对象):

class Frag : Fragment() {

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

        var itemModels: List<ItemModel>? = null

        if(PathManager.shared.app?.items != null) {  // NULL - WHY?
            itemModels = PathManager.shared.app?.items  
        }
   }
}

总结发生的事情:

  1. AppModel 已在 PathManager() 中正确构建。数据在那里(可以 在调试器中查看)

  2. 路径管理器启动 'Frag()' class

  3. Frag() class 引用回在中创建的伴生对象 PathManager(),试图在中查找数据 'PathManager.shared.app?.items',但是里面的数据是null,当 不应该。伴随对象是否与静态对象相同 变量?

我确定我遗漏了一些东西,只是在寻求建议。我是否正确调用了伴生对象?我不能像这样从新的 class 引用它吗?给出了什么?

PathManager.shared.app?.items 始终为空,因为您永远不会将 AppModel 分配给伴随对象。 var app: AppModel? = null PathManager 中的实例 class 和 PathManager.shared.app 是两个不同的对象。

要存储静态数据,您可以执行以下操作:

open class PathManager() {

    companion object {
        //Here Don't create object is you dont need it 
        //var shared = PathManager()    // SHARED OBJECT IN QUESTION

        //Create AppModel instance.
        var mAppModel: AppModel? = null
    }

    //Comment this as will manage companion object.
    //var app: AppModel? = null

    fun setUp() {
        // Show loading...

        // Parse the JSON
        val gson = GsonBuilder().create()
        val text = mainActivity.resources.openRawResource(R.raw.jsonFile)  
            .bufferedReader().use { it.readText() }
        val appModel = gson.fromJson(text, AppModel::class.java)

        mAppModel = appModel

        performInitialPath()
    } 

    private fun performInitialPath() {

        val app = app?.let { it } ?: error("Attempted to start without an App Config.")

        mAppModel = app    // DEBUGGER SHOWS CORRECT VALUES HERE

        // Removed irrelevant code for readability, builds 'initialFoo' here

        this.mainActivity.addFragment(initialFoo)
    }
}

并在其他地方使用它 class

class Frag : Fragment() {

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

        var itemModels: List<ItemModel>? = null
        //Retrive the list from shared object.
        if(PathManager.mAppModel?.items != null) {  // NULL - WHY?
            itemModels = PathManager.mAppModel?.items  
        }
   }
}