变量被声明为var,但编译器称其为val [英] Variable is declared as var but compiler is saying it val

查看:104
本文介绍了变量被声明为var,但编译器称其为val的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译器给我错误

错误:(97,17)无法重新分配Val

Error:(97, 17) Val cannot be reassigned

,但变量声明为var.

编辑:您可以在我的代码中看到注释.当我分配 rcv = recyclerView
chkStrictSearch =复选框,我在上面得到红色下划线,并带有上面的错误消息工具提示

You can see comments in my code. When i assign rcv = recyclerView and
chkStrictSearch = checkBox I get red underline here with above error message tooltip

下面是我的代码:

private var rcv: RecyclerView? = null
private var chkStrictSearch: android.widget.CheckBox? = null  

private fun getMainView(): View{
    return with(context){
        frameLayout{
            lparams(width = matchParent, height = matchParent)
            //Error is below - val cannot be reassign
            rcv = recyclerView{
                lparams(width = matchParent, height = matchParent)
                setPadding(0, resources.getDimension(R.dimen.toolbar_height).toInt(), 0, dip(48))
                clipToPadding = false
            }
           //and here - val cannot be reassign
            chkStrictSearch = checkBox{
                text = "Strict Search"
            }.lparams(width = wrapContent, height = wrapContent){
                marginEnd = dip(24)
                bottomMargin = dip(50)
                gravity = Gravity.BOTTOM
            }
        }
    }
}  

推荐答案

似乎是静态代码分析中的错误,或者可能是由增量编译引起的.尝试重建/清理项目.

Seems like a bug in the static code analysis or it may be caused by incremental compiling. Try to rebuild/clean the project.

或尝试以下操作:

private fun getMainView(): View {
    return with(context) {
        frameLayout {
           rcv = null
        }
    }
}

如果该编译现在可以正常工作,请添加回原始代码,然后再次编译.

If the compilation works now, add back your original code and compile again.

这篇关于变量被声明为var,但编译器称其为val的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆