The variable is declared as var, but the compiler says that val

The compiler gives me an error

Error: (97, 17) Val cannot be reassigned

but the variable is declared as var .

Edit: You can see the comments in my code. When I assign rcv = recyclerView and
chkStrictSearch = checkBox I get a red underline here with a tooltip with an error message

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 } } } } 
0
source share
1 answer

It looks like an error in parsing static code, or it could be caused by incremental compilation. Try rebuilding / cleaning the project.

Or try the following:

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

If compilation is working now, add the source code back and compile again.

+2
source

Source: https://habr.com/ru/post/1243821/


All Articles