当语法无法获取值时,为什么要Kotlin?

时间:2019-12-30 07:48:44

标签: android kotlin

我只想问为什么我在Logcat中的变量uploadImgsPath总是说:“ lateinit属性uploadImgsPath尚未初始化”?

为什么uploadImgsPath不能从语法中获取值?

companion object {

        lateinit var categoryDesignID:String
        lateinit var uploadImgsPath:StorageReference
    }

    //Set Spinner
            categoryViewModel.getMainCategoryDesign().observe(this, Observer {
                if (it[0].designTitle != "UnCategorized") {
                    it.add(0, CategoryDesign("", "", "UnCategorized"))
                }
                spinner.adapter = ArrayAdapter<CategoryDesign>(
                    context!!,
                    android.R.layout.simple_spinner_item,
                    it)
                    .apply {
                        setDropDownViewResource(android.R.layout.simple_dropdown_item_1line)
                    }
                spinner.setSelection(0, false)
                spinner.onItemSelectedListener= object : AdapterView.OnItemSelectedListener {
                    override fun onNothingSelected(parent: AdapterView<*>?) {
                        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                    }

                    override fun onItemSelected(
                        parent: AdapterView<*>?,
                        view: View?,
                        position: Int,
                        id: Long
                    ) {
                        when (it.get(position).categoryDesignID) {
                            //Setup firebase storage path
                            "" -> {
                                categoryDesignID = ""
                                Toast.makeText(context,"Please choose a category",Toast.LENGTH_LONG).show()}
                            "ClothingDesign" -> {uploadImgsPath = mStorageRef
                                .child("uploadedDesignImgs")
                                .child("ClothingDesign")
                            categoryDesignID = "ClothingDesign" }

                            "CommercialDesign" -> {uploadImgsPath = mStorageRef
                                .child("uploadedDesignImgs")
                                .child("CommercialDesign")
                            categoryDesignID = "CommercialDesign"}

                            "LightingDesign" -> {uploadImgsPath = mStorageRef
                                .child("uploadedDesignImgs")
                                .child("LightingDesign")
                            categoryDesignID = "LightingDesign"}
                        }
                    }
                }
            })

2 个答案:

答案 0 :(得分:0)

您没有显示实际访问<div class='box'></div>的代码,所以据我们所知,您只是在.box{ width:500px; height:500px; background: #eb01a5;/*fallback*/ background-image:url("https://images.pexels.com/photos/3370704/pexels-photo-3370704.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"); /* fallback */ background-image: linear-gradient(rgba(255, 0, 0, 0), rgba(217, 19, 93, 0.73)), url('https://images.pexels.com/photos/3224164/pexels-photo-3224164.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500'); } 执行并赋值之前访问它。

但是,并非uploadImgsPath中的所有代码路径都为when分配了一个值:

when

所以这也可能是个问题。

答案 1 :(得分:0)

谢谢大家。 后来我找到了原因,并且已经解决了这个问题。 以上代码完全正确。 问题是因为我将Firestore的文档ID设置为模型的字段ID, 因此,当syntex无法匹配两个不同的ID时,让var uploadImgsPath无法获取任何值。

相关问题