I can't able to use findViewById('R.id.textView').setText("message")
in my code, Android studio shows this error
Cannot resolve method 'setText(java.lang.String)
It is not the same as this question Android: Why can I not setText in the same line as findViewById as in updated android versions we no longer need to do casting for findViewById()
method, for more details see this question No need to cast the result of findViewById?
Above all, the same code works if I save the result of findViewById
in a variable first(Without casting it to TextView manually) i.e
TextView textview = findViewById(R.id.textview);
textview.setText("message");
Above code works fine, if findViewById returns View
object then it should also not work, and if it returns TextView
object then one linear code findViewById('R.id.textView').setText("message")
should work as well.
I know if I cast it in TextView then it will work. but my question is why do I need to cast if it already returns TextView object? thanks!