Variable is declared as var but compiler is saying

2019-08-29 12:04发布

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条回答
▲ chillily
2楼-- · 2019-08-29 12:41

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.

查看更多
登录 后发表回答