Variable is declared as var but compiler is saying it val

半腔热情 提交于 2020-01-07 04:36:07

问题


Compiler is giving me the error

Error:(97, 17) Val cannot be reassigned

but the variable is declared as var.

Edit: 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

Below is my code:

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
            }
        }
    }
}  

回答1:


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

Or try this:

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

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



来源:https://stackoverflow.com/questions/44800225/variable-is-declared-as-var-but-compiler-is-saying-it-val

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!