Cast result findViewById android method

后端 未结 5 734
无人共我
无人共我 2020-12-03 12:46

Why do we always have to cast the value returned by the method findViewById(id) ? The method already returns a view, as I\'ve seen in google reference :

5条回答
  •  猫巷女王i
    2020-12-03 13:31

    View is the common superclass of other view classes such as ImageView. In object-oriented polymorphism, you can substitute a specific instance e.g. ImageView with the superclass. This is useful in generic APIs such as findViewById() return value.

    Downcasting to a more specific type in Java requires an explicit cast.

提交回复
热议问题