Why this type casting?

前端 未结 2 1908
情歌与酒
情歌与酒 2021-01-14 08:21

I am using an intent to start another activity, and making my intent carry some data as an extra to the newly created activity. I am following a tutorial to do that.

相关标签:
2条回答
  • 2021-01-14 08:50

    findViewById returns a View. If you need to use methods of a derived class you need to cast. If you need to use only the methods of the base class you can avoid the cast

    0 讨论(0)
  • 2021-01-14 09:05

    The point is that findViewById(int id) returns a generic View object. This method doesn't parse your xml in order to understand what kind of type is your view. It just makes a new View object from the relationship put in place by your R.java file, built up by your IDE (Eclipse, I suppose).

    You need to cast the result of findViewById(int id) because you're not casting an EditText object, your casting a View, of which EditText is only a specification.

    0 讨论(0)
提交回复
热议问题