How to fix Parameter specified as non-null is null on rotating screen in a Fragment while creating a Spinner in Kotlin?

Add ? in

view = View?

Here is code

spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
        override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) {
            val num = when (spinner.selectedItem.toString()) {
                "H" -> editText.setText("1")
                "He" -> editText.setText("4")
                "C" -> editText.setText("12")
                "O" -> editText.setText("16")
                else -> editText.setText("")
            }
        }

It is correct, annotating the "view" parameter as nullable does appear to correct the issue.

It appears that the Kotlin generated code is incorrect as the auto-generated code marks "view" without the nullable attribute.

Tags:

Android

Kotlin