Android Studio findViewById throws error when searching EditText

I am very new to Android Development and am just learning some very simple things. In my MainActivity.java file, I am trying to use the EditText I created in the XML file. In the XML file, I have set the id by specifying:

android:id=@+id/ageRecieved

      

When I go to the java file and print:

EditText ageInput = findViewById(R.id.ageRecieved);

      

then a popup message appears saying "Incompatible types. Required: android.widget.EditText Found: android.view.View

Very confused why he does it. Any help would be great. Thanks to

+3


source to share


3 answers


try this:

EditText ageInput = (EditText) findViewById(R.id.ageRecieved);

      



you need to provide your opinion.

+3


source


The method findViewById(int id)

, as the name says, finds (and returns) a view with a given identifier. So it returns a View object and it returns to the programmer to convert the View to a more specialized object: in your case you have to convert it to an EditTextView, otherwise jvm throws an error because you cannot assign the View object to the EditTextView reference. hope this was helpful bye!



0


source


findViewById returns a View object when you want to work with an EditText, TextView or other child View object that you have to execute.

EditText et = (EditText) findViewById(R.id.ageRecieved);
TextView tv = (TextView) findViewById(R.id.ageRecieved);

      

0


source







All Articles