Skip to content Skip to sidebar Skip to footer

Android: Why Can I Not Settext In The Same Line As Findviewbyid

I am not sure if this is an Android issue or a AndroidStudio issue. Why is this valid: TextView currentItem currentItem = (TextView)findViewById(R.id.myText); currentItem.setText('

Solution 1:

findViewById returns a generic 'View', hence you have to cast it to TextView before you can set the text. In your piece of code you are trying to invoke setText on the 'View' object rather than the 'TextView' object.

Try this :

 ((TextView)findViewById(R.id.myText)).setText("text");

Solution 2:

Try this please :

((TextView)findViewById(R.id.myText)).setText("text");

Solution 3:

findViewById returns a View, so you don't have setText from TextView yet. You have to cast first to TextView by putting parenthesis around findViewById and then call setText like this:

((TextView)findViewById(R.id.myText)).setText("text");

Also, this is a Java thing, not Android nor Android Studio thing.

Post a Comment for "Android: Why Can I Not Settext In The Same Line As Findviewbyid"