In my layout that is called activity_main.xml
, I have two TextView. I can access it from my MainActivity by using a variable
binding = ActivityMainBinding.inflate(layoutInflater)
in this way:
binding.textView1
binding.textView2
In my code binding.textView1
returns an object TextView
but the second returns an object TextView?
.
That forces me to access the second TextView with ?.
operator like
binding.textView2?.text = "HelloWorld"
because using the .
operator cause the error
Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type TextView?
So the questions are there a method for defining a View not nullable in my layout? How is possible that two objects with the same property had different behavior?
The object was nullable because it was present not in all XML layouts, so in some layouts it was null.