Skip to content Skip to sidebar Skip to footer

Why This Type Casting?

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. This data is a

Solution 1:

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.

Solution 2:

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

Post a Comment for "Why This Type Casting?"